【问题标题】:Set a trigger for first item in the list为列表中的第一项设置触发器
【发布时间】:2016-08-21 14:44:49
【问题描述】:

我的问题是另一个帖子的后续问题:What are the different triggers in WPF?

我想设置一个触发器,如果​​该项目是列表视图中的第一个则触发(这样我可以添加额外的文本)。我应该使用哪种类型的触发器?

这是我的代码:

<ListView Grid.Row="1" Grid.Column="2" Name="contactList" Margin="0,0,0,0">
<ListView.ItemTemplate>
    <DataTemplate>
        <WrapPanel>
            <TextBlock Text="{Binding Name}" />
            <TextBlock>
                <TextBlock.Style>
                    <Style TargetType="{x:Type TextBlock}">
                        <Style.Triggers>
                            <Trigger <!-- what do I have to put here so it triggers when the item is the first one in the list? -->>
                                <Setter Property="Text" Value=" - this is the first item in the list!!"/>
                                <Setter Property="Foreground" Value="#7f8c8d"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
        </WrapPanel>
    </DataTemplate>
</ListView.ItemTemplate>
</ListView>

【问题讨论】:

    标签: c# wpf triggers


    【解决方案1】:

    您必须使用DataTriggerMultiBinding 以及这个坏男孩的转换器。看看吧。

    首先,您的转换器...

    class IsFirstItemConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            ObservableCollection<NamedObject> col = (ObservableCollection<NamedObject>)values[0];
            if (col[0].Equals(values[1]))
                return true;
            else
                return false;
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    现在在 App.xaml 中引用它...

    <local:IsFirstItemConverter x:Key="isFirstItem"/>
    

    现在是肉...

    <ListView x:Name="lv" ItemsSource="{Binding Items}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock>
                    <TextBlock.Style>
                        <Style TargetType="{x:Type TextBlock}">
                            <!--Text property moved to here-->
                            <Setter Property="Text" Value="{Binding Name}"/>
                            <Style.Triggers>
                                <DataTrigger Value="True">
                                    <DataTrigger.Binding>
                                        <MultiBinding Converter="{StaticResource isFirstItem}">
                                            <MultiBinding.Bindings>
                                                <Binding ElementName="lv" Path="ItemsSource"/>
                                                <Binding/>
                                            </MultiBinding.Bindings>
                                        </MultiBinding>
                                    </DataTrigger.Binding>
                                    <Setter Property="Text" Value="First item"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    请注意,您必须将 Text 属性的设置器移动到样式中。由于控件中的显式声明会覆盖样式中的任何属性设置器,因此除非您按所示移动它,否则您将永远看不到触发器的效果。希望这会有所帮助!

    【讨论】:

    • 感谢 gengis_jhan。我已经这样做了,但它在 Convert 方法中触发了一个未处理的异常。这是一个例外:在 KnxPmpApp.exe 中发生了“System.InvalidCastException”类型的未处理异常附加信息:无法将“MS.Internal.NamedObject”类型的对象转换为“System.Windows.Controls.ListView”类型。它发生在这一行:ListView lv = (ListView)values[0];
    • 您的绑定是否按照上面显示的顺序进行?通过 values[] 数组传入的对象是什么?
    • values[0] is - [0] {DependencyProperty.UnsetValue} object {MS.Internal.NamedObject} values[1] 是第一个联系人
    • 您的ListViewx:Name 属性是否与第一个绑定的ElementName 属性相同?
    • 不是。这就是问题所在。解决了:)谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 2022-07-08
    • 1970-01-01
    相关资源
    最近更新 更多