I\'m using the excellent MVVM Light Toolkit. My ViewModel exposes:
public const string CourtCodesTypeCourtPropertyName = \"CourtCodesTypeCourt\";
private Li
Here I have found answer http://cinch.codeplex.com/discussions/239522
For the DataGridComboBoxColumn you have to create a StaticRecource of the ItemsSource like:
<CollectionViewSource Source="{Binding Element=theView, Path=DataContext.ViewModelCollection1}" x:Key="ViewModelCollection1" />
and bind it to the DataGridComboBoxColumn with following:
ItemsSource="{Binding Source={StaticResource ViewModelCollection1}}"
Thats because the DataGridColumns are not a part of the visual tree.
And if you want to bind on a collection of a item of the DataGrid you have to set the ItemsSource over the two styles:
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=ModelCollection1}" />
</Style> </DataGridComboBoxColumn.ElementStyle> <DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=ModelCollection1}" />
</Style> </DataGridComboBoxColumn.EditingElementStyle>
Check out this brilliant tutorial/example: https://code.msdn.microsoft.com/windowsdesktop/Best-ComboBox-Tutorial-5cc27f82
DataGrid
column definitions don't participate in the logical tree in the way you would expect. It's ridiculous, but last I checked you have to do something like this:
<DataGridComboBoxColumn Header="CourtType" SelectedItemBinding="{Binding Type}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.CourtCodesTypeCourt}"/>
<Setter Property="IsReadOnly" Value="True"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.CourtCodesTypeCourt}"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
You'll notice I've also changed your TextBinding
to a SelectedItemBinding
. I'm not sure if you actually intended a TextBinding
, but if you just want to allow the user to select between the list, then SelectedItemBinding
is likely what you want.
Also, your VMs don't exactly follow best practices. You're using List<T>
instead of ObservableCollection<T>
, and you're exposing it as List<T>
rather than something simpler such as ICollection<T>
.