开发者

Programatically add ValidationRules to WPF DataGrid when autogenerating columns

开发者 https://www.devze.com 2022-12-08 04:00 出处:网络
I want to do this in the AutoGeneratingColumn event: <my:DataGridTextColumn Header=\"CompanyName\">

I want to do this in the AutoGeneratingColumn event:

<my:DataGridTextColumn Header="CompanyName">
    <my:DataGridTextColumn.Binding>
        <Binding Path="CompanyName">
            <Binding.ValidationRules>
                <local:DataRowValidation ValidationStep="UpdatedValue" />
            </Binding.ValidationRules>
        </Binding>
    </my:DataGridTextColumn.Binding>
</my开发者_如何学Python:DataGridTextColumn>

Is it possible? The columns are autogenerated so I just want to add the ValidationRules to Binding.


I ended up with this, if no one has any better solution.

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

精彩评论

暂无评论...
验证码 换一张
取 消