在 int 列 DataGrid 的情况下,IDataErrorInfo 不会触发

发布于 2024-10-31 00:36:02 字数 1312 浏览 0 评论 0原文

我有一个 DataGrid,其中的列与“int”类型属性绑定。我正在使用 IDataErrorInfo 进行验证。我的验证规则工作正常,一旦我更改单元格的值,它们就会被解雇。假设用户输入的值小于 0,我会在工具提示中显示相应的错误。当用户清除文本框的值时就会出现问题。在这种情况下, IDataErrorInfo 永远不会触发,因此我无法通过工具提示显示用户验证错误,说明该值不能为空。 DataGrid 在文本框周围设置红色边框,这很好,还有一个“!”在行标题处签名,但工具提示中没有验证错误,因为 IDataErrorInfo 从未被触发。如果文本框的值被清除,我可以采取什么措施来触发 IDataError 信息?

编辑:
XAML

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding DisplayOrder, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, ValidatesOnDataErrors=True}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <TextBox Text="{Binding DisplayOrder, UpdateSourceTrigger=PropertyChanged}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

DisplayOrder 属性

public int DisplayOrder
{
    get
    {
        return m_DisplayOrder;
    }
    set
    {
        if(value != m_DisplayOrder)
        {
            m_DisplayOrder = value;
            OnPropertyChanged("DisplayOrder");
        }
    }
}

I have a DataGrid with a Column that is binded with an 'int' type property. I am using IDataErrorInfo for validation. My validation rules are working fine they get fired as soon as I change value of a cell. Let's say user enter value less than 0, I show respective error in the tooltip. Problem comes when user clear the value of text box. In that case IDataErrorInfo never fires and as a result I cannot show user validation error through tooltip saying that value cannot be empty. DataGrid makes the red border around textbox, which is fine, and a '!' sign at row header, but no validation error in the tooltip as IDataErrorInfo never got fired. Is there anything I can do to fire IDataError info in case value of textbox is cleared?

Edit:
XAML

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding DisplayOrder, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, ValidatesOnDataErrors=True}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <TextBox Text="{Binding DisplayOrder, UpdateSourceTrigger=PropertyChanged}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

DisplayOrder Property

public int DisplayOrder
{
    get
    {
        return m_DisplayOrder;
    }
    set
    {
        if(value != m_DisplayOrder)
        {
            m_DisplayOrder = value;
            OnPropertyChanged("DisplayOrder");
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心安伴我暖 2024-11-07 00:36:02

您应该发布您的绑定并向我们展示您的 itemssource 对象。我假设您绑定的属性是 int 类型,因此如果您清除文本框,wpf 绑定会尝试将 int 属性设置为 null。由于您的 int 不可为空,因此发生了绑定错误。并且由于绑定错误,您的 idataerror 没有机会触发,因为它不会被调用。您可以尝试将 ValidatesOnExceptions=true 添加到数据网格列绑定中以查看错误。

you should post your bindings and show us your itemssource object. i assume that your property you bind to is type of int, so if you clear the textbox, wpf binding try to set the int property to null. there a binding error happens cause your int is not nullable. and because of the binding error your idataerror has no chance to fire cause it would not be invoked. you can try to add ValidatesOnExceptions=true to your datagrid column binding to see an error.

╰◇生如夏花灿烂 2024-11-07 00:36:02

正如您从错误中看到的,StringToNumber 转换正在抛出,您可以尝试使用自己的 IValueConverter 将 String 转换为 Int 或其他方式。也许默认情况下它会将空字符串转换为 0 (其中您的 IDataErrorInfo 应该启动并显示错误)

As you can see from error, StringToNumber converting is throwing, can u try to use your own IValueConverter to convert String to Int and other way around. and maybe by default it will convert empty string to 0 (where your IDataErrorInfo should kick in and show error)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文