Reputation: 21
:) I have one DataGrid. In Column2 there is "ComboBox1", in Column3 there is "ComboBox2". I would like to set IsEnabled to false on "ComboBox2" as long as "ComboBox1" has SelectedIndex=0. For each row seperately.
This works perfectly outside the Datagrid with two ComboBoxes (with the help of Style and DataTrigger). However, inside the Datagrid, within Column3 I cannot "see" "ComboBox1" ("Cannot find source for binding...").
It's basically a namescope issue. However, referring to a Combobox within a DataGrid by name seems to be wrong in the first place. So: any ideas how to accomplish this?
Thank you very much in advance!
<Window.Resources>
<CollectionViewSource x:Key="Source1" Source="{Binding List1}" />
<CollectionViewSource x:Key="Source2" Source="{Binding List2}" />
</Window.Resources>
<DataGrid x:Name="ModelControl" AutoGenerateColumns="False" ItemsSource="{Binding List3}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding MyName}" Header="Modellname" />
<DataGridTemplateColumn Header="Header 1">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="ComboBox1" DisplayMemberPath="MyName" SelectedIndex="0">
<ComboBox.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource Source2}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Header 2">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="ComboBox2" DisplayMemberPath="MyName">
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="IsEnabled" Value="True" />
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedIndex,ElementName=ComboBox1}" Value="0">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
<ComboBox.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource Source1}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Please don't mind the CompositeCollection.
Upvotes: 2
Views: 1533
Reputation: 169150
However, referring to a Combobox within a DataGrid by name seems to be wrong in the first place. So: any ideas how to accomplish this?
You should bind the SelectedIndex
property to a source property of your model class:
<DataGrid x:Name="ModelControl" AutoGenerateColumns="False" ItemsSource="{Binding List3}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding MyName}" Header="Modellname" />
<DataGridTemplateColumn Header="Header 1">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="ComboBox1" DisplayMemberPath="MyName"
SelectedIndex="{Binding YourIndexProperty}">
<ComboBox.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource Source2}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Header 2">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="ComboBox2" DisplayMemberPath="MyName">
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="IsEnabled" Value="True" />
<Style.Triggers>
<DataTrigger Binding="{Binding YourIndexProperty}" Value="0">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
<ComboBox.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource Source1}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Make sure that the model type implements the INotifyPropertyChanged
interface and raise the PropertyChanged
event in the setter: https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx
Upvotes: 1
Reputation: 726
try something like this:
Modify your combobox1 by adding the selected id SelectedValue="{Binding List3ComboBox1ItemId}"
, the List3ComboBox1ItemId
property must be a property of list3
<ComboBox x:Name="ComboBox1" DisplayMemberPath="MyName" SelectedValue="{Binding List3ComboBox1ItemId}">
<ComboBox.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource Source2}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
and your combobox2 you have to include a converter to verify your id, if id > 0 returns true. (not included in this answer)
<ComboBox x:Name="ComboBox2" DisplayMemberPath="MyName" SelectedItem="{Binding List3ComboBox2Item}" IsEnabled={Binding List3ComboBox1ItemId, Converter={StaticResource IntegerToBoolConverter}} >
Upvotes: 0