Reputation: 19598
I have WPF DataGrid, where I am displaying some data. I need to provide an option for the user to edit the data, if he/she checks the checkbox in the header. Here is the WPF part I have done.
<DataGrid.Columns>
<DataGridTextColumn Header="MaxLength" Binding="{Binding MaxLength}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<CheckBox x:Name="AAOverride" Content="Increase Max Length" />
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
</DataGrid.Columns>
I would like to enable / disable the MaxLength Column, based on user selection. I am following MVVM, so codebehind option I don't have :(
Upvotes: 0
Views: 159
Reputation: 102743
Just to a RelativeSource binding to get to your root view-model. Something like this, I suppose (also needs an IValueConverter to negate the bool for "increase max length" -> "read only"):
<DataGrid.Columns>
<DataGridTextColumn Header="MaxLength" Binding="{Binding MaxLength}"
IsReadOnly="{Binding
RelativeSource={RelativeSource AncestorType=DataGrid},
Path=DataContext.IsMaxLengthEnabled,
Converter={StaticResource NegateBool}}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<CheckBox x:Name="AAOverride" Content="Increase Max Length"
IsChecked="{Binding RelativeSource={RelativeSource AncestorType=DataGrid},
Path=DataContext.IsMaxLengthEnabled,
Mode=TwoWay}"
/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
</DataGrid.Columns>
Upvotes: 0