C
C
cicatrix2021-03-17 13:39:24
WPF
cicatrix, 2021-03-17 13:39:24

How to pass the type of the associated element to the converter (converter)?

Hello!

There is a DataGrid, the data source for which changes during execution. Code for illustration:

// Один из типов
public class Foo
{
   public string Id {get; set;}
   public string Data {get; set;}
}

// Другой тип с другим набором полей и их типов
public class Bar
{
   public int Code {get; set;}
   public string OtherData {get; set;}
}

// Позже...
// Имеем два (или более) различных источников данных для одного и того же DataGrid
List<Foo> fooList = new List<Foo> { new Foo() { Id="ABC", Data = "data1" }}
List<Bar> barList = new List<Bar> { new Bar() { Code=3, OtherData = "data2" }}

// Переключаем источники
if (condition)
    this.dgDisplay.ItemsSource = fooList;
else
    this.dgDisplay.ItemsSource = barList;


Now in the XAML, I need to pass not only the name of the property to the Converter, but also the type of data currently being used (Foo or Bar) from the example above.
That is, in the XAML code I have the following fragment:
<DataGrid.ColumnHeaderStyle>
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="Content" Value="{Binding ConverterParameter=????, Converter={StaticResource PropertyDescriptionConverter}}" />
    </Style>
</DataGrid.ColumnHeaderStyle>


The question is how to pass the type (Foo or Bar) to the Converter along with the property name?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question