I have a situation where ItemsSource property of a DataGrid is an array of integers. These integers are the keys to a corresponding dictionary. What I need to display in columns are the fields, that are members of the dictionary values objects.
<DataGrid ItemsSource="{Binding Path=thisDataContext, ElementName=control, Mode=OneWay}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="0.3*" Binding="{Binding Converter={StaticResource IntToPersonConverter}, Mode=OneWay }" />
<DataGridTextColumn Header="Department" Width="0.3*" Binding="{Binding Path=DepartmentIDX, Converter={StaticResource DepartmentConverter}, Mode=OneWay }" />
<DataGridTextColumn Header="Position" Width="0.3*" Binding="{Binding Path=EmployeeTypeIDX, Converter={StaticResource EmployeeTypeConverter}, Mode=OneWay }" />
</DataGrid.Columns>
</DataGrid>
The object of a class Person I’m working with have Properties – Department and Position ids, so I’ve implemented multiple Converters – Person to Department name and Person to Postition name. I have no idea how to combine all of them. I thought that would be possible if I could set DataContext properties to the DataGrid Rows. So I could pass a Person object to a Convertor.
There’s no much point in exercising WPF futures such as binding converters etc just for the sake of proving that they work – they do. The number of calls to converter is going to be massive, which isn’t elegant at least and is easily avoidable. I’d suggest that you pre-process your collection before setting grid’s ItemsSource.
I would personally use LINQ projectsions, and establish the dictionaries as static members of your class. Give us a shout and I’ll post a sample.
Update 1 – Sample Code