17

为什么我不能这样编码

<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

我在做什么错请帮助。

4

3 回答 3

33

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

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

在边框上使用单一样式,此样式的设置器代表您的“正常”外观。此样式还包含您的 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>
于 2010-08-26T16:59:14.953 回答
20

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.

于 2010-08-26T14:52:30.583 回答
0

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

例子:

 <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

于 2018-05-12T11:54:09.023 回答