【问题标题】:Inconsistent behavior of Setters in WPF Style Triggers in conjunction with Local Values [duplicate]WPF样式触发器中的设置器与本地值的行为不一致[重复]
【发布时间】:2016-12-31 13:11:02
【问题描述】:

我正在为我的 WPF 应用程序使用自定义 TextBox 控件,该控件在 Generics.xaml 文件中设置了默认样式。我在文件中设置的Template包括以下Setter和Triggers:

<Setter Property="Background" Value="Blue" />

<Trigger Property="IsFocused" Value="True">
    <Setter Property="Background" Value="Yellow"/>
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="BorderBrush" Value="Black"/>
</Trigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
    <Condition Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}}" Value="True" />
    <Condition Binding="{Binding Path=IsFocused, RelativeSource={RelativeSource Self}}" Value="False" />
    <Condition Binding="{Binding Path=(Validation.HasError), RelativeSource={RelativeSource Self}}" Value="False" />
    <Condition Binding="{Binding Path=Text, RelativeSource={RelativeSource Self}, Converter={StaticResource IsEmptyStringConverter}}" Value="False" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
    <Setter Property="Background" Value="Green" TargetName="Border" />
    <Setter Property="Foreground" Value="White"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>

如果我没有为 Background 属性设置本地值,此代码将按预期工作。当聚焦时,我的背景变成黄色,当没有聚焦并且没有验证错误时,它变成绿色。

但是,如果我为 Background 属性设置 Local Value,就像在下面的代码 sn-p 中一样,当触发其中一个触发器时,Background 行为会非常奇怪。

&lt;custom:TextBox Background="Orange" /&gt;

  • 当 IsFocued 触发器被触发时,背景保持橙色。我认为这是由于 WPF 的 Dependency Property Setting Precedence 导致的,它偏爱本地值而不是样式触发器中设置的值。是的,随着 BorderBrush 和 Foreground 的变化,Trigger 肯定会被触发。
  • 当第二个触发器(MultiDataTrigger for Validation)被触发时,背景变为绿色。本地值被忽略。

据我所知,这是不一致的。为什么第一个触发器使用我的本地值,而第二个触发器坚持它的背景设置器?这两个触发器之间是否有不同的优先级?

【问题讨论】:

  • 背景设置器的 isFocused 触发器中缺少 TargetName="Border",因此您的 isfocused 和多数据触发器很可能针对不同的背景
  • 谢谢!当我添加属性时,选中时背景变为黄色。这是否意味着当我针对 Setter 中的特定控件时,该值不会被本地值覆盖?
  • 基本上可以这样想,当您设置本地值时,您的文本框控件只是子控件的集合,您在定位“边框”时设置基本控件的属性,您指定一个属性覆盖基本控件的子控件

标签: c# .net wpf wpf-controls wpf-style


【解决方案1】:

样式触发器模板触发器无法更改由本地值设置的值 但他们可以使用 TemplatedParent 模板

更改该控件的子项的继承属性

例子:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Background" Value="Red"></Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Green"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <Grid>
        <Button Width="100" Height="100" Background="Pink"></Button>
    </Grid>
</Window>

在示例中,按钮背景属性将保持粉红色(本地值),您可以看到使用Snoop

但是,如果您将鼠标悬停在按钮上,您会看到背景正在发生变化,这是因为按钮的子项(边框)被其父级 TemplatedParent 和 TemplatedParent.Trigger 更改。再次使用Snoop查看值源

【讨论】:

    猜你喜欢
    • 2019-07-22
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 2012-03-14
    • 2018-01-28
    • 2010-10-15
    相关资源
    最近更新 更多