在样式中混合 MultiBinding 转换器和触发器时出现问题 [英] Issue while mixing MultiBinding converter and Trigger in style

查看:29
本文介绍了在样式中混合 MultiBinding 转换器和触发器时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

中设置样式(假设转换器返回红色)

Setting the style in <UserControl.Resources> (assuming the converter returns the color Red)

<Style x:Key="FieldToValidate" TargetType="{x:Type TextBox}">
    <Setter Property="Background">
        <Setter.Value>
            <MultiBinding Converter="{StaticResource VisualQueueOnErrorConverter}">
                <Binding RelativeSource="{RelativeSource self}" Path="Name" />
                <Binding RelativeSource="{RelativeSource AncestorType={x:Type DockPanel}}" Path="DataContext.ErrorFieldName" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" />
            </MultiBinding>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="Background" Value="Red">
            <Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource self}}" />
            <Setter Property="Foreground" Value="White" />
        </Trigger>
    </Style.Triggers>
</Style>

和控制:

<TextBox  Name="FirstName" Text="{Binding FirstName}" Style="{StaticResource FieldToValidate}">  

预期的结果是当 MultiBinding 转换器将背景颜色更改为红色时,字段 FirstName 获得焦点并且前景色更改为白色,但是,当字段的背景更改为红色时,它没有获得焦点,也没有获得新的前景色.

The expected result is for the field FirstName to get the focus and the foreground color changed to white when the MultiBinding converter changes the background color to Red but, while the field's background changes to Red, it doesn't get the focus nor the new foreground color.

似乎 XAML 解析器在处理 Trigger 之前属性设置器的 MultiBinding 转换器.

It almost seems like the XAML parser processes the Trigger before the property setter's MultiBinding converter.

欢迎提出任何建议!

推荐答案

我认为触发器中指定的Red画笔和转换器返回的Red画笔不是被认为是相等的(因为它们是不同的实例),所以触发器永远不会执行.无论如何,依靠背景颜色来触发某些事情似乎不是一个好主意......

I think the Red brush specified in the trigger and the Red brush returned by the converter are not considered equal (because they are different instances), so the trigger never executes. Anyway, it doesn't seem a very good idea to rely on the background color to trigger something...

您应该更改转换器,使其在发生错误时返回 true,并按如下方式使用它:

You should change your converter so that it returns true when an error occurs, and use it as follows:

<Style x:Key="FieldToValidate" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <DataTrigger Value="True">
            <DataTrigger.Binding>
                <MultiBinding Converter="{StaticResource VisualQueueOnErrorConverter}">
                    <Binding RelativeSource="{RelativeSource self}" Path="Name" />
                    <Binding RelativeSource="{RelativeSource AncestorType={x:Type DockPanel}}" Path="DataContext.ErrorFieldName" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" />
                </MultiBinding>
            </DataTrigger.Binding>
            <Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource self}}" />
            <Setter Property="Background" Value="Red" />
            <Setter Property="Foreground" Value="White" />
        </DataTrigger>
    </Style.Triggers>
</Style>

此外,在您的转换器名称中,您可能指的是视觉提示",而不是队列" ;)

Also, in the name of your converter, you probably meant "visual cue", not "queue" ;)

这篇关于在样式中混合 MultiBinding 转换器和触发器时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆