[英]Setting the DataGridTextColumn IsReadonly property from DataGridTextColumn.HeaderTemplate Checkbox
I have WPF DataGrid, where I am displaying some data. 我有WPF DataGrid,在这里显示一些数据。 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.
这是我完成的WPF部分。
<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. 我想根据用户选择启用/禁用MaxLength列。 I am following MVVM, so codebehind option I don't have :(
我正在关注MVVM,所以我没有codebehind选项:(
Just to a RelativeSource binding to get to your root view-model. 只需绑定到RelativeSource绑定即可进入您的根视图模型。 Something like this, I suppose (also needs an IValueConverter to negate the bool for "increase max length" -> "read only"):
我想是这样的(还需要一个IValueConverter来否定布尔值以“增加最大长度”->“只读”):
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.