如何触发此错误模板?

发布于 2024-10-21 08:26:13 字数 2509 浏览 5 评论 0原文

下面是一个从绑定角度工作的模板,但错误模板不会显示,并且如果没有 AdornedElementPlaceholder,结果看起来有点花哨。

我的视图模型实现了 IDataErrorInfo,通常我会通过将 ValidatesOnError=True 作为绑定的一部分来触发错误模板。此特定视图模型仅用于显示,因此永远不会调用 IDataErrorInfo 索引器。不过,我确实有许多与验证相关的有用属性,包括布尔值 IsValid 属性以及 IDataErrorInfo.Error,这两个属性都可以正确响应无效的视图模型。

我应该将错误转换为 ValidationResult 并以这种方式触发它吗?或者有什么更容易的事情吗?

干杯,
Berryl

当前模板

<!-- FooterViewModel DataTemplate -->
<DataTemplate DataType="{x:Type model:FooterViewModel}">

    <Label x:Name="lblTotalTime"
        Style="{StaticResource FooterStyle}" 
        Content="{Binding TotalTime, Converter={StaticResource TotalAmountConv}}" >
        <Label.ToolTip>
            <TextBlock Text="{Binding FeedbackMessage}" ></TextBlock>
        </Label.ToolTip>
        <Validation.ErrorTemplate>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <TextBlock DockPanel.Dock="Right" Text=" *" 
                               Foreground="Red" 
                               FontWeight="Bold" FontSize="16" 
                               />
                    <Border BorderBrush="Red"  BorderThickness="1">
                        <AdornedElementPlaceholder Name="placeholder"></AdornedElementPlaceholder>
                    </Border>
                </DockPanel>
            </ControlTemplate>
        </Validation.ErrorTemplate>
    </Label>

    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding IsValid}" Value="False">
            <Setter TargetName="lblTotalTime" Property="Control.BorderBrush" Value="Red"/>
            <Setter TargetName="lblTotalTime" Property="Control.BorderThickness" Value="1"/>
            <Setter TargetName="lblTotalTime" Property="Control.Background" Value="LightYellow"/>
        </DataTrigger>
    </DataTemplate.Triggers>

</DataTemplate>

更新

好吧,我只需更改绑定以包含 ValidatesOnErrors 即可启动 IDataErrorInfo,但错误模板仍然不显示。

这是绑定

    <ItemsControl 
        ItemsSource="{Binding Path=FooterViewModels, Mode=OneWay, ValidatesOnDataErrors=True}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>

Below is a template that works from a binding perspective, but the error template doesn't show, and without an AdornedElementPlaceholder the result looks a bit garish.

My view models implement IDataErrorInfo, and normally I would trigger the error template by having ValidatesOnError=True as part of my binding. This particular view model is display only, so the IDataErrorInfo indexer is never invoked. I do have a number of useful properties related to validation though, including a boolean IsValid property as well as IDataErrorInfo.Error, both of which properly respond to the view model being invalid.

Should I translate the error to a ValidationResult and trigger it that way? Or is there something easier?

Cheers,
Berryl

current template

<!-- FooterViewModel DataTemplate -->
<DataTemplate DataType="{x:Type model:FooterViewModel}">

    <Label x:Name="lblTotalTime"
        Style="{StaticResource FooterStyle}" 
        Content="{Binding TotalTime, Converter={StaticResource TotalAmountConv}}" >
        <Label.ToolTip>
            <TextBlock Text="{Binding FeedbackMessage}" ></TextBlock>
        </Label.ToolTip>
        <Validation.ErrorTemplate>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <TextBlock DockPanel.Dock="Right" Text=" *" 
                               Foreground="Red" 
                               FontWeight="Bold" FontSize="16" 
                               />
                    <Border BorderBrush="Red"  BorderThickness="1">
                        <AdornedElementPlaceholder Name="placeholder"></AdornedElementPlaceholder>
                    </Border>
                </DockPanel>
            </ControlTemplate>
        </Validation.ErrorTemplate>
    </Label>

    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding IsValid}" Value="False">
            <Setter TargetName="lblTotalTime" Property="Control.BorderBrush" Value="Red"/>
            <Setter TargetName="lblTotalTime" Property="Control.BorderThickness" Value="1"/>
            <Setter TargetName="lblTotalTime" Property="Control.Background" Value="LightYellow"/>
        </DataTrigger>
    </DataTemplate.Triggers>

</DataTemplate>

UPDATE

Ok, I am getting IDataErrorInfo to kick in just by changing my binding to include ValidatesOnErrors, BUT the error template still does not show up.

Here is the binding

    <ItemsControl 
        ItemsSource="{Binding Path=FooterViewModels, Mode=OneWay, ValidatesOnDataErrors=True}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>

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

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

发布评论

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

评论(1

情绪少女 2024-10-28 08:26:14

默认情况下,仅在更新绑定的源时才运行验证。在您的 ItemsControl.ItemsSource 绑定中,源是您的 FooterViewsModels,显然它永远不会更新(因为您有 Mode=OneWay)。

您可以使用 DataErrorValidationRule.ValidatesOnTargetUpdated当目标更新时也运行验证。该链接给出了一个例子。

请记住,Binding.ValidatesOnDataErrors 属性只是将 DataErrorValidationRule 实例添加到 Binding.ValidationRules 集合的快捷方式。

最后,定义绑定的控件将具有 验证。错误。在您的情况下,这是 ItemsControl,而不是其中的项目。因此,我相信您需要将 DataErrorValidationRule 添加到您的 Label.Content 绑定中。或者您需要在 ItemsControl 上定义 ErrorTemplate,具体取决于您的用途。

By default, the validation is only run when the Source of the binding is updated. In your ItemsControl.ItemsSource binding the Sources is your FooterViewsModels, which obviously will never be updated (because you have Mode=OneWay).

You can use the DataErrorValidationRule.ValidatesOnTargetUpdated to run the validation when the target is updated as well. The link gives an example.

Keep in mind that the Binding.ValidatesOnDataErrors property is is just a short cut for adding an instance of DataErrorValidationRule to the Binding.ValidationRules collection.

Finally, the control that the binding is defined one will have the Validation.Errors. In your case, that is the ItemsControl, not the items inside it. So, I believe you need to add the DataErrorValidationRule to your Label.Content binding. Or you need to define your ErrorTemplate on the ItemsControl, depending on what you are going for.

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