I have a simple DataGrid
with two columns like
<DataGrid ItemsSource="{Binding Commands}" CanUserAddRows="True" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Value" Binding="{Binding Value}"/>
<DataGridTemplateColumn Header="Command">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding ComboItems}" SelectedValue="{Binding SelectedItem}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource ItemConverter}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
My goal is to disable the cell with the ComboBox
of the new row. The following picture shows my DataGrid
(bound to a ObservableCollection
of two items) with the column to disable marked.
I tried already to use a converter to disable the ComboBox
IsEnabled="{Binding Value, Converter={StaticResource DisableConverter}}"
but the converter don't get's called until I entered a value inside the first column.
Hope anybody can help me!
You could apply the following Style
to the ComboBox
:
<ComboBox ItemsSource="{Binding ComboItems}" SelectedValue="{Binding SelectedItem}">
<ComboBox.Style>
<Style TargetType="ComboBox">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=DataContext,RelativeSource={RelativeSource AncestorType=DataGridRow}}"
Value="{x:Static CollectionView.NewItemPlaceholder}">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource ItemConverter}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>