问题而混合MultiBinding转换器和触发风格 [英] Issue while mixing MultiBinding converter and Trigger in style

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

问题描述

在选择风格< UserControl.Resources> (假设转换器返回的颜色红)

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转换器更改背景颜色为红色,但是,而本场的背景变为红色,它没有得到关注的焦点,也没有新的前景色。

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分析器处理触发的之前属性setter的MultiBinding转换器。

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

任何和所有的建议表示欢迎!

Any and all suggestions welcome!

推荐答案

我觉得触发器和指定的红色刷红由转换器返回的刷子是不相等的(因为它们是不同的实例),所以触发器从未执行。无论如何,这似乎不是一个很好的主意,靠背景颜色来触发的东西...

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>

另外,在转换器的名字,你可能是指视觉的提示,而不是队列;)

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

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