简体   繁体   English

是否可以更改 WPF 中验证错误模板的属性?

[英]Is it possible to change properties on the validation error template in WPF?

I'm using the Validation.ErrorTemplate property in my XAML view to display an icon next to any invalid control.我在我的 XAML 视图中使用 Validation.ErrorTemplate 属性在任何无效控件旁边显示一个图标。

For one particular control in my view, I need to set the icon in a slightly different position.在我看来,对于一个特定的控件,我需要将图标设置在稍微不同的位置。 Is it possible for me to use the same control template, but somehow update the Margin property for a certain control?我是否可以使用相同的控件模板,但以某种方式更新某个控件的Margin属性?

Here's my (abridged) code:这是我的(删节)代码:

<UserControl>
    <UserControl.Resources>
        <ControlTemplate x:Key="ValidationTemplate" TargetType="Control">
            <DockPanel>
                <Grid
                    DockPanel.Dock="Right"
                    Height="16"
                    Margin="10,0,0,0"
                    VerticalAlignment="Center"
                    Width="16">
                    <Image
                        AutomationProperties.AutomationId="_validationIcon"
                        Source="{x:Static icons:Icons.ValidationIcon}"
                        ToolTip="{Binding Path=ErrorContent}" />
                </Grid>
                <AdornedElementPlaceholder />
            </DockPanel>
        </ControlTemplate>

        <ItemsControl Validation.ErrorTemplate="{StaticResource ValidationTemplate}" />
</UserControl>

The only way I've managed to achieve what I need is to create a new ControlTemplate just for the control that requires a different icon placement.我设法实现所需的唯一方法是为需要不同图标位置的控件创建一个新的ControlTemplate I would rather reuse my original control template if possible.如果可能的话,我宁愿重用我原来的控制模板。

I had exactly same requirement in my old project.我在旧项目中有完全相同的要求。 I solved it using an attached dependency property:我使用附加的依赖属性解决了它:

public static class ErrorTemplateProperties
{
    public static readonly DependencyProperty ErrorMarginProperty = DependencyProperty.RegisterAttached
    (
        "ErrorMargin",
        typeof(Thickness),
        typeof(ErrorTemplateProperties),
        new FrameworkPropertyMetadata(new Thickness(10,0,0,0))
    );

    public static Thickness GetErrorMargin(DependencyObject obj)
    {
        return (Thickness)obj.GetValue(ErrorMarginProperty);
    }

    public static void SetErrorMargin(DependencyObject obj, Thickness value)
    {
        obj.SetValue(ErrorMarginProperty, value);
    }
}

add it to ValidationTemplate:将其添加到 ValidationTemplate:

<ControlTemplate x:Key="ValidationTemplate" TargetType="Control">
    <DockPanel>
        <Grid
            DockPanel.Dock="Right"
            Margin="{Binding Path=AdornedElement.(local:ErrorTemplateProperties.ErrorMargin), ElementName=ui}"
            Height="16"
            VerticalAlignment="Center"
            Width="16">
            <Image
                AutomationProperties.AutomationId="_validationIcon"
                Source="{x:Static icons:Icons.ValidationIcon}"
                ToolTip="{Binding Path=ErrorContent}" />
        </Grid>

        <AdornedElementPlaceholder x:Name="ui"/>
    </DockPanel>
</ControlTemplate>

and then optionally change on required ui elements:然后可选地更改所需的 ui 元素:

<ItemsControl helpers:ErrorTemplateProperties.ErrorMargin="0,0,0,0" 
              Validation.ErrorTemplate="{StaticResource ValidationTemplate}" />

I also found a workaround with DynamicResources but attached DP are more flexible and laconic in my opinion我还找到了DynamicResources 的解决方法,但我认为附加的 DP 更加灵活和简洁

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM