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

DataGrid的RowValidationRule在ComboBox选项变更时未触发问题

解决DataGrid中ComboBox切换时RowValidationRule不触发的问题

我明白你遇到的困扰了——当你在DataGrid的ComboBox列切换选项时,行验证规则SaveComponentValidationRule完全没反应,只有修改DataGridTextColumn里的文本内容时才会触发验证。咱们从几个关键地方入手解决这个问题:

1. 核心问题:模型未实现INotifyPropertyChanged

你的SelectedFileModel类没有实现属性变化通知接口,这意味着当你修改RelatedFileType(ComboBox绑定的属性)时,DataGrid根本不知道这个属性发生了变化,自然不会触发行级验证。

修复模型代码

给SelectedFileModel添加INotifyPropertyChanged实现:

public class SelectedFileModel : INotifyPropertyChanged
{
    private FileType _relatedFileType;
    private string _fileName;
    private string _filePath;

    public FileType RelatedFileType
    {
        get => _relatedFileType;
        set
        {
            if (_relatedFileType != value)
            {
                _relatedFileType = value;
                OnPropertyChanged(nameof(RelatedFileType));
            }
        }
    }

    public string FileName
    {
        get => _fileName;
        set
        {
            if (_fileName != value)
            {
                _fileName = value;
                OnPropertyChanged(nameof(FileName));
            }
        }
    }

    public string FilePath
    {
        get => _filePath;
        set
        {
            if (_filePath != value)
            {
                _filePath = value;
                OnPropertyChanged(nameof(FilePath));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

2. 修复CellEditEnding事件的干扰

你当前在CellEditEnding事件里直接禁用了编辑元素,这会打断DataGrid的验证流程——验证还没来得及触发,编辑元素就被禁用了。可以用Dispatcher延迟执行禁用操作,让验证先完成:

private void SelectedFilesDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    // 延迟到后台优先级执行,确保验证流程完成
    Dispatcher.BeginInvoke(new Action(() =>
    {
        e.EditingElement.IsEnabled = false;
    }), System.Windows.Threading.DispatcherPriority.Background);
}

3. 强制触发行验证(可选兜底方案)

如果上面的修改后还是有问题,可以给ComboBox添加SelectionChanged事件,手动触发当前行的验证:

修改XAML中的ComboBox

<DataTemplate x:Key="comboTemplate">
    <ComboBox IsSynchronizedWithCurrentItem="False" 
              ItemsSource="{Binding DataContext.FileDataTypes, ValidatesOnDataErrors=True, RelativeSource={RelativeSource AncestorType=local:SaveComponentView},UpdateSourceTrigger=PropertyChanged}" 
              SelectedValue="{Binding RelatedFileType, UpdateSourceTrigger=PropertyChanged}" 
              IsEnabled="{Binding IsEditing, RelativeSource={RelativeSource AncestorType=DataGridCell}}"
              SelectionChanged="ComboBox_SelectionChanged"/>
</DataTemplate>

添加后台事件处理和辅助方法

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var comboBox = sender as ComboBox;
    var dataGridCell = FindVisualParent<DataGridCell>(comboBox);
    if (dataGridCell == null) return;

    var dataGridRow = FindVisualParent<DataGridRow>(dataGridCell);
    if (dataGridRow != null)
    {
        // 提交行编辑,强制触发行验证规则
        dataGridRow.CommitEdit(DataGridEditingUnit.Row, true);
    }
}

// 辅助方法:查找视觉树中的父元素
private T FindVisualParent<T>(DependencyObject child) where T : DependencyObject
{
    var parentObject = VisualTreeHelper.GetParent(child);
    if (parentObject == null) return null;
    
    return parentObject as T ?? FindVisualParent<T>(parentObject);
}

验证SaveComponentValidationRule的ValidationStep

你当前设置的ValidationStep="UpdatedValue"是正确的(源更新后触发验证),但要确保你的规则逻辑是基于整个行数据的,而不是仅针对文本列的属性。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:13:15