我正在寻找一种方法来禁用我的一些细胞在一个WPF DataGrid。因为isReadOnly不是DependencyProperty,所以我不能使用它。
还有别的办法吗?
我想写这样的东西:
<DataGridTextColumn IsReadOnly="{Binding Path=Value,Converter={StaticResource ValueToBooleanConverter}}" /> 但是无论如何,如何做到这一点都会很好,因为我没有看到一种方法,但是将数据分割到不同的DataGrid's中。
发布于 2013-12-02 15:18:09
如果您可以t make a column read-only, you can go to a cell level. For example by makingStyle``并将其添加到必需的列中:
<DataGridTextColumn Binding="{Binding TextProperty}">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="IsEnabled"
Value="{Binding Path=Value,Converter={StaticResource ValueToBooleanConverter}}" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>我在这里创建了Style内部列,但当然,它可以被移动到资源中,并按键在任何所需列中引用(但您必须是shure转换器,因为这种样式是可访问的)。CellsIsReadOnlydoesn似乎没有一个策划者,所以我在这里做得很好。
发布于 2021-01-05 17:16:54
您可以使用DataGrid.OnBeginningEdit事件:
void DataGrid_OnBeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
DataGridRow row = e.Row;
var viewModel = (YourViewModel) row.Item;
if (<Test to check if it is the expected column>
&& viewModel.Value == <?>) e.Cancel = true;
}这不会破坏MVVM。该行为由视图模型的属性(可以测试的内容)驱动,并且只有基本代码将属性绑定到视图。
https://stackoverflow.com/questions/20330925
复制相似问题