recently we started to use WPF at work. Now I want to create a DataGrid from a list of objects (DataGrid ItemSource) that contains a project role, the employee that should do the job and a list of employees that could also do that job. Lets call this list the "MainList". In that DataGrid is a ComboBox Column that uses another list of objects as ItemSource where you can change the employee for the job. I will call this list the "ChildList". This list is included in the MainList (as allready mentioned) and I bind it by using the correct BindingPath. So far so good. Now I have to set the SelectedItem (to show which Employee is currently selected). From the MainList I can get the employee that should be selected from the ChildList. Obviously I cant do this by Binding. Unfortunatly I cant get the SelectedItem property in the code-behind. Basically I need to go through every row from the DataGrid and get the Item that should be selected in the ComboBox. Then I would go through the ComboBox Items until I find the matching Item and set this as SelectedItem. But I cant find a way to do this.
I tried using a DataGridComboBoxColumn aswell but it has only the SelectedItemBinding property and since you cant compare while binding this should not work. I also tried to get every cell in the code-behind, that is a ComboBox but without success so far.
<DataGrid x:Name="DgvProjectTeam" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="False" Margin="0" RowHeight="40" CanUserAddRows="False" BorderThickness="1" VerticalScrollBarVisibility="Auto" HorizontalGridLinesBrush="#FFA2B5CD" VerticalGridLinesBrush="#FFA2B5CD" ItemsSource="{Binding}" VirtualizingStackPanel.IsVirtualizing="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Resource" Width="200" x:Name="DgtProjectCoreTeam">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="CbxResource" ItemsSource="{Binding Path=ListOfPossibleResources}" DisplayMemberPath="ResourceOfQMatrix.Fullname"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
The DataGrid shows everything I need. I just dont know how I can set the SelectedItem for each of the generated ComboBoxCells in the code-behind.
Anyone an idea?
GridRow
wrapper (a generically typed one) can be used to provide the additional properties the grid can manage such asSelectedItem
- this then can be bound to and you then can example your bound viewmodel rather than write grid specific code-behind. – Charleh