将验证错误传递给 WPF 中的 UI 元素?

发布于 2024-08-25 05:18:22 字数 1259 浏览 5 评论 0原文

我正在使用 IDataErrorInfo 验证 WPF 表单中的数据。我在演示者中实施了验证。

实际的验证正在发生,但应该更新 UI 并设置样式的 XAML 并未发生。

问题是:

  <Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"
                    Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                Path=(Validation.Errors)[0].ErrorContent}"/>
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
        </Style.Triggers>
    </Style>

我对 Validation.Errors 的绑定不包含任何数据。如何从 Presenter 类获取此数据并将其传递给此 XAML 以更新 UI 元素?

编辑:

文本框:

 <TextBox Style="{StaticResource textBoxInError}" Name="txtAge" Height="23" Grid.Row="3" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Width="150">
            <TextBox.Text>
                <Binding Path="StrAge" Mode="TwoWay"
                         ValidatesOnDataErrors="True"
                         UpdateSourceTrigger="PropertyChanged"/>
            </TextBox.Text>

验证发生,但数据无效时应用的样式没有发生。

I am using IDataErrorInfo to validate my data in a form in WPF. I have the validation implemented in my presenter.

The actual validation is happening, but the XAML that's supposed to update the UI and set the style isn't happening.

Here it is:

  <Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"
                    Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                Path=(Validation.Errors)[0].ErrorContent}"/>
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
        </Style.Triggers>
    </Style>

The problem is that my binding to Validation.Errors contains no data. How do I get this data from the Presenter class and pass it to this XAML so as to update the UI elements?

EDIT:

Textbox:

 <TextBox Style="{StaticResource textBoxInError}" Name="txtAge" Height="23" Grid.Row="3" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Width="150">
            <TextBox.Text>
                <Binding Path="StrAge" Mode="TwoWay"
                         ValidatesOnDataErrors="True"
                         UpdateSourceTrigger="PropertyChanged"/>
            </TextBox.Text>

The validation occurs, but the style to be applied when data is invalid is not happening.

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

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

发布评论

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

评论(2

毁梦 2024-09-01 05:18:22

您是否在绑定表单时观察过输出窗口?通过在绑定发生时检查输出可以发现大量验证问题。

还有一个快速说明:

使用

Path=(Validation.Errors).CurrentItem.ErrorContent

而不是

Path=(Validation.Errors)[0].ErrorContent

当向控件提供有效值时,它将为您节省一些进一步的绑定异常

Have you watched the output window as your form is being bound? a significant number of validation issues can be found by reviewing the output as the binding occurs.

One quick note as well:

use

Path=(Validation.Errors).CurrentItem.ErrorContent

rather than

Path=(Validation.Errors)[0].ErrorContent

It will save you some further binding excecption when a valid value is provided to the control

各空 2024-09-01 05:18:22

我注意到你的风格还没有完全完成。

该样式需要一个定义“Validation.ErrorTemplate”的控件模板,以便在发生验证错误时起作用。尝试进行以下更改,看看效果如何。

Paul Stovell 在此处有一篇关于 WPF 验证的非常好的文章,其中将介绍大多数你需要的东西。我还在此处编写了一篇文章来简化您可能也喜欢的验证。

之前

<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"
                Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                Path=(Validation.Errors)[0].ErrorContent}"/>
            <Setter Property="Background" Value="Red"/>
        </Trigger>
    </Style.Triggers>
</Style>

之后

<Style  x:Key="textBoxInError" TargetType="{x:Type TextBox}">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <Border BorderBrush="Red" BorderThickness="1">
                    <AdornedElementPlaceholder />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"
                Value="{Binding RelativeSource={RelativeSource Self}, 
                       Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
    </Style.Triggers>
</Style>

I noticed that your Style is not completely finished.

The Style needs a control template that defines a "Validation.ErrorTemplate" for it to work when a validation error occurs. Try making the following changes to see how it goes.

Paul Stovell has a very good article on WPF validation here that will cover most things you need. I have also written an article here to simplify validation that you might also like.

BEFORE

<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"
                Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                Path=(Validation.Errors)[0].ErrorContent}"/>
            <Setter Property="Background" Value="Red"/>
        </Trigger>
    </Style.Triggers>
</Style>

AFTER

<Style  x:Key="textBoxInError" TargetType="{x:Type TextBox}">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <Border BorderBrush="Red" BorderThickness="1">
                    <AdornedElementPlaceholder />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"
                Value="{Binding RelativeSource={RelativeSource Self}, 
                       Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
    </Style.Triggers>
</Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文