简体   繁体   English

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

[英]WPF DataGrid disable cell edit based on value

I have a WPF DataGrid populated with an ObservableCollection<ZeroConnection> named ZeroTable . 我有一个WPF DataGrid其中ObservableCollection<ZeroConnection>名为ZeroTableObservableCollection<ZeroConnection> If ZeroTable.NetID == 0 the DataGrid row should be disabled and that works. 如果ZeroTable.NetID == 0 ,则应禁用DataGrid行并且该行有效。 Furthermore if ZeroTable.Number == "" editing of that particular cell should be disabled. 此外,如果ZeroTable.Number == "" ,则应禁止对该特定单元格进行编辑。 But have not succeeded to implement this as the user still can edit the cell. 但是尚未成功实现此操作,因为用户仍然可以编辑单元格。 My XAML below; 我的XAML在下面;

<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>

And the C# code below; 以及下面的C#代码;

public class CellReadOnlyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string number = (string)value;
        if (number == "")
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new Exception("The method or operation is not implemented.");
    }
}

public class RowReadOnlyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int netID = (int) value;
        if (netID == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new Exception("The method or operation is not implemented.");
    }
}

The cell "number" that contains an empty string is still editable. 包含空字符串的单元格“数字”仍可编辑。 The background of the cell does change so the DataTrigger seems to be activated. 单元格的背景确实发生了变化,因此DataTrigger似乎已激活。 Help is appreciated 感谢帮助

You need EditingElementStyle to mark the field enabled/disabled 您需要EditingElementStyle以将字段标记为启用/禁用

Code below shows how to do it 下面的代码显示了如何做

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM