You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xceed DataGrid单元格编辑器绑定对应行及Email编辑权限控制问题

解决Xceed DataGrid单元格绑定与编辑权限控制的方案

嘿,我之前也碰到过类似的Xceed DataGrid单元格编辑权限控制的问题,折腾了好一会儿才搞定,给你分享几个可行的方案:

方案1:通过CellStyle控制单元格只读状态

这是最简单的方式,直接在Email列的CellStyle里绑定IsReadOnly属性到行数据的AllowEmailEdit(注意要取反,因为IsReadOnly=true代表不可编辑)。

首先确保你的业务实体(比如叫Customer)已经实现了INotifyPropertyChanged,这样属性变化时UI能自动更新:

public class Customer : INotifyPropertyChanged
{
    private string _email;
    private bool _allowEmailEdit;

    public string Email
    {
        get => _email;
        set { _email = value; OnPropertyChanged(); }
    }

    public bool AllowEmailEdit
    {
        get => _allowEmailEdit;
        set { _allowEmailEdit = value; OnPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

然后在XAML里配置DataGrid的Email列:

<xcdg:DataGridControl ItemsSource="{Binding Customers}">
    <xcdg:DataGridControl.Columns>
        <!-- 其他列 -->
        <xcdg:Column FieldName="Email" Title="邮箱">
            <xcdg:Column.CellStyle>
                <Style TargetType="{x:Type xcdg:Cell}">
                    <Setter Property="IsReadOnly" 
                            Value="{Binding DataContext.AllowEmailEdit, 
                                    RelativeSource={RelativeSource AncestorType={x:Type xcdg:DataRow}},
                                    Converter={StaticResource BooleanNotConverter}}" />
                </Style>
            </xcdg:Column.CellStyle>
        </xcdg:Column>
    </xcdg:DataGridControl.Columns>
</xcdg:DataGridControl>

这里用到了BooleanNotConverter,如果你的项目里没有,可以自己写一个简单的转换器:

public class BooleanNotConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return !(bool)value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return !(bool)value;
    }
}

记得在资源里声明这个转换器:

<Window.Resources>
    <local:BooleanNotConverter x:Key="BooleanNotConverter" />
</Window.Resources>

方案2:自定义CellEditor控制编辑器可用性

如果你需要更精细的控制(比如只禁用编辑器而不是整个单元格),可以自定义CellEditor,然后绑定编辑器的IsEnabled属性:

<xcdg:Column FieldName="Email" Title="邮箱">
    <xcdg:Column.CellEditor>
        <xcdg:CellEditor>
            <xcdg:CellEditor.EditTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding}" 
                             IsEnabled="{Binding DataContext.AllowEmailEdit, 
                                     RelativeSource={RelativeSource AncestorType={x:Type xcdg:DataRow}}}" />
                </DataTemplate>
            </xcdg:CellEditor.EditTemplate>
        </xcdg:CellEditor>
    </xcdg:Column.CellEditor>
</xcdg:Column>

这个方案里,单元格在非编辑状态下还是正常显示,只有进入编辑模式时,TextBox才会根据AllowEmailEdit决定是否可用。

关键注意点

  • 一定要用RelativeSource指向xcdg:DataRow,因为单元格默认的DataContext是当前单元格的字段值(也就是Email字符串),而我们需要的是整个行的业务实体,DataRow的DataContext才是你的Customer对象。
  • 确保业务实体实现了INotifyPropertyChanged,这样当AllowEmailEdit的值动态变化时,UI能实时响应。

内容的提问来源于stack exchange,提问作者obratim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:01:27