在自动生成列时以编程方式将ValidationRules添加到WPF DataGrid

时间:2009-10-13 10:11:31

标签: wpfdatagrid autogeneratecolumn validationrules

我想在AutoGeneratingColumn事件中执行此操作:

<my:DataGridTextColumn Header="CompanyName">
    <my:DataGridTextColumn.Binding>
        <Binding Path="CompanyName">
            <Binding.ValidationRules>
                <local:DataRowValidation ValidationStep="UpdatedValue" />
            </Binding.ValidationRules>
        </Binding>
    </my:DataGridTextColumn.Binding>
</my:DataGridTextColumn>

有可能吗?这些列是自动生成的,所以我只想将ValidationRules添加到Binding。

1 个答案:

答案 0 :(得分:2)

如果没有人有更好的解决方案,我最终会这样做。

private void DataGridAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    var binding = (Binding)((DataGridBoundColumn)e.Column).Binding;
    binding.ValidationRules.Add(new DataRowValidationRule { ValidationStep = ValidationStep.UpdatedValue });
}