wpf 为什么不能将 DataTrigger 添加到控件的 Triggers 集合中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3576193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:04:31  来源:igfitidea点击:

Why can't I add a DataTrigger to my control's Triggers collection?

wpfdatatrigger

提问by Tan

Why cant I code like this

为什么我不能这样编码

<Border Width="130" Height="70">
    <Border.Triggers>
        <DataTrigger Binding="{Binding Path=CurrentStatus}" Value="0">
            <Setter Property="Style" Value="{StaticResource ResourceKey=ListBoxItemBorder}"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=CurrentStatus}" Value="200">
            <Setter Property="Style" Value="{StaticResource ResourceKey=ListBoxItemBorderInactive}"/>
        </DataTrigger>
    </Border.Triggers>
</Border>

I get this error

我收到这个错误

Failed object initialization (ISupportInitialize.EndInit). 
Triggers collection members must be of type EventTrigger.  
Error at object '4_T' in markup file

What am I doing wrong plz help.

我在做什么错请帮忙。

回答by Scott

Abe is correct and explains the limitations well. One thing you might want to consider is:

安倍是对的,并且很好地解释了局限性。您可能需要考虑的一件事是:

Instead of having two border styles, and trying to pick between them based on a trigger...

而不是有两种边框样式,并试图根据触发器在它们之间进行选择......

Use a single style on your border, this style's setters represent your 'normal' look. This style also contains your DataTrigger, and your DataTrigger has a collection of setters which essentially represents your second style (which have higher priority than the standard setters when this trigger evaluates to true!

在您的边框上使用单一样式,此样式的设置器代表您的“正常”外观。此样式还包含您的 DataTrigger,并且您的 DataTrigger 具有一组 setter,它们基本上代表您的第二个样式(当此触发器评估为 true 时,它​​们的优先级高于标准 setter!

Edit:

编辑:

Something like this -

像这样的东西——

<Style TargetType="Border" x:Key="BorderStyle">
    <!-- These setters are the same as your normal style when none of your triggers are true -->
    <Setter Property="BorderBrush" Value="Black" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=CurrentStatus}" Value="0">
            <!-- These setters are the same as your ListBoxItemBorder style -->
            <Setter Property="BorderBrush" Value="Green" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=CurrentStatus}" Value="200">
            <!-- These setters are the same as your ListBoxItemBorderInactive style -->
            <Setter Property="BorderBrush" Value="Gray" />
        </DataTrigger>
    </Style.Triggers>
</Style>

回答by Abe Heidebrecht

Unfortunately, only EventTriggerscan be applied directly to elements. If you want to use a Triggeror DataTrigger, they have to be in a Style, ControlTemplate, or DataTemplate.

不幸的是,只能EventTriggers直接应用于元素。如果你想使用一个Trigger或者DataTrigger,他们是在一个StyleControlTemplateDataTemplate

From the resource names, it looks like this is a Borderinside a ListBoxItemControlTemplate. You could easily move the triggers into the template's triggers collection.

从资源名称来看,这似乎是一个Border内部的ListBoxItemControlTemplate. 您可以轻松地将触发器移动到模板的触发器集合中。

回答by Ali Yousefi

Here is a way for no limitations triggers.

这是一种无限制触发器的方法。

Example:

例子:

 <Border Width="130" Height="100" Grid.Row="1">
        <ListBox x:Name="lstItems" ItemsSource="{Binding TestItems}">

        </ListBox>
        <tg:TriggerExtensions.Triggers>
            <tg:TriggerCollections>
                <tg:DataTriggerInfo Binding="{Binding CurrentStatus}" Value="0">
                    <tg:DataTriggerInfo.Setters>
                        <tg:SetterInfo ElementName="lstItems" Property="Style" Value="{StaticResource ListBoxRed}"/>
                    </tg:DataTriggerInfo.Setters>
                </tg:DataTriggerInfo>
                <tg:DataTriggerInfo Binding="{Binding CurrentStatus}" Value="0" IsInvert="True">
                    <tg:DataTriggerInfo.Setters>
                        <tg:SetterInfo ElementName="lstItems" Property="Style" Value="{StaticResource ListBoxBlue}"/>
                    </tg:DataTriggerInfo.Setters>
                </tg:DataTriggerInfo>
            </tg:TriggerCollections>
        </tg:TriggerExtensions.Triggers>
    </Border>

Link Sample

链接示例

Link Component Github

链接组件Github