WPF DataGrid禁用基于值的单元格编辑

时间:2018-12-13 20:23:04

标签: c# wpf

我有一个WPF DataGrid,其中装有名为ObservableCollection<ZeroConnection>的{​​{1}}。如果ZeroTable的{​​{1}}行应被禁用并且可以正常工作。此外,如果应该禁止对该特定单元格进行ZeroTable.NetID == 0编辑。但是尚未成功实现此操作,因为用户仍然可以编辑单元格。我的XAML在下面;

DataGrid

以及下面的C#代码;

ZeroTable.Number == ""

包含空字符串的单元格“数字”仍可编辑。单元格的背景确实发生了变化,因此<Window.Resources> <local2:RowReadOnlyConverter x:Key="RowReadOnlyConverter" /> <local2:CellReadOnlyConverter x:Key="CellReadOnlyConverter" /> </Window.Resources> <DataGrid Name="ZeroTableGridView" Grid.Column="0" AlternatingRowBackground="#FFEEEEEE" AutoGenerateColumns="False" ItemsSource="{Binding ZeroTable}" CanUserAddRows="False" dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True" dd:DragDrop.DropTargetAdornerBrush="Coral" dd:DragDrop.DropHandler="{Binding }"> <DataGrid.Resources> <Style TargetType="{x:Type DataGridRow}"> <Style.Triggers> <DataTrigger Binding="{Binding NetID, Converter={StaticResource RowReadOnlyConverter}}" Value="True"> <Setter Property="IsEnabled" Value="False" /> </DataTrigger> </Style.Triggers> </Style> </DataGrid.Resources> <DataGrid.Columns> <DataGridTextColumn Header="Nummer" Binding="{Binding Number}" CanUserSort="False" IsReadOnly="False"> <DataGridTextColumn.ElementStyle> <Style TargetType="{x:Type TextBlock}"> <Style.Triggers> <DataTrigger Binding="{Binding Number, Converter={StaticResource CellReadOnlyConverter}}" Value="True"> <Setter Property="IsEnabled" Value="False" /> <Setter Property="Background" Value="HotPink"/> <!-- For debugging --> </DataTrigger> </Style.Triggers> </Style> </DataGridTextColumn.ElementStyle> </DataGridTextColumn> <DataGridTextColumn Header="Ledning" Binding="{Binding Wire}" CanUserSort="False" IsReadOnly="False"/> <DataGridTextColumn Header="Postbeteckning" Binding="{Binding ItemDes}" CanUserSort="False" IsReadOnly="True"/> <DataGridTextColumn Header="Uttag" Binding="{Binding Terminal}" CanUserSort="False" IsReadOnly="True"/> <DataGridTextColumn Header="Hänvisning" Binding="{Binding Ref}" CanUserSort="False" IsReadOnly="True"/> <DataGridTextColumn Header="Kretsschema" Binding="{Binding Sheet}" CanUserSort="False" IsReadOnly="False"/> <DataGridTextColumn Header="Anmärkning" Binding="{Binding Remark}" CanUserSort="False" IsReadOnly="False"/> <!--<DataGridTextColumn Header="ID" Binding="{Binding NetID}" CanUserSort="False"/> --> </DataGrid.Columns> </DataGrid> 似乎已被激活。感谢帮助

1 个答案:

答案 0 :(得分:0)

您需要EditingElementStyle将该字段标记为启用/禁用

下面的代码显示了操作方法

<DataGridTextColumn Header="Nummer" Binding="{Binding Number}" CanUserSort="False" IsReadOnly="False">
    <DataGridTextColumn.EditingElementStyle>
        <Style TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Number}" Value="">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Number}" Value="{x:Null}">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>