为什么我无法将 DataTrigger 添加到控件的触发器集合中?

发布于 2024-09-16 02:49:18 字数 794 浏览 8 评论 0原文

为什么我不能这样编码

<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>

我收到此错误

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

我做错了什么请帮忙。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稀香 2024-09-23 02:49:26

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

示例:

 <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>

链接示例

链接组件Github

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

一场信仰旅途 2024-09-23 02:49:24

不幸的是,只有EventTriggers可以直接应用于元素。如果您想使用TriggerDataTrigger,它们必须采用StyleControlTemplate或<代码>数据模板。

从资源名称来看,它看起来像是 ListBoxItem ControlTemplate 内的 Border。您可以轻松地将触发器移至模板的触发器集合中。

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

From the resource names, it looks like this is a Border inside a ListBoxItem ControlTemplate. You could easily move the triggers into the template's triggers collection.

弃爱 2024-09-23 02:49:22

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

不要使用两种边框样式,并尝试根据触发器在它们之间进行选择...

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

编辑:

类似这样的 -

<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>

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!

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文