WPF 列表框项触发器不起作用

本文关键字:触发器 不起作用 列表 WPF | 更新日期: 2023-09-27 18:34:34

我正在使用列表框项样式和触发器来更改鼠标悬停颜色,但样式似乎不起作用:

<ListBox ItemsSource="{Binding LocationItems}" SelectionChanged="RadListBox_SelectionChanged" Name="RLBLocations">
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="ListBoxItem">
                                <Setter Property="BorderBrush" Value="#FF709A70"/>
                                <Setter Property="Height" Value="50"/>
                                <Setter Property="Width" Value="200"/>
                                <Setter Property="Foreground" Value="#FF5C5C5C"/>
                                <Setter Property="FontFamily" Value="Franklin Gothic Book"/>
                                <Setter Property="FontSize" Value="16"/>
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                            <StackPanel Orientation="Horizontal" Height="50">
                                                <Image Source="{Binding Icon}" Margin="0" Stretch="UniformToFill" Width="32" Height="32"/>
                                                <TextBlock Margin="10,10,0,0" Text="{Binding Text.Text}" Foreground="Black" TextAlignment="Left"/>
                                            </StackPanel>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="ToolTip" Value="{Binding ToolTip}"/>
                                <Setter Property="BorderThickness" Value="1"/>
                                <Setter Property="Background" Value="#FFF0F0F0"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding
                            RelativeSource={RelativeSource
                                Mode=FindAncestor,
                                AncestorType={x:Type ListBoxItem}},
                                Path=IsMouseOver}" Value="True">
                                        <Setter Property="Background" Value="#FF709A70"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </ListBox.ItemContainerStyle>
                    </ListBox>

我在输出窗口中收到此错误:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ListBoxItem', AncestorLevel='1''. BindingExpression:Path=IsMouseOver; DataItem=null; target element is 'ListBoxItem' (Name=''); target property is 'NoTarget' (type 'Object')

我知道这个错误,但需要帮助尝试找出样式 - 我尝试使用数据模板执行此操作,但也没有成功。

WPF 列表框项触发器不起作用

您的数据

触发器:

 <DataTrigger Binding="{Binding RelativeSource={RelativeSource
                                                    Mode=FindAncestor,
                                                    AncestorType={x:Type ListBoxItem}},
                                  Path=IsMouseOver}" 
                Value="True">
     <!-- ... -->
  </DataTrigger>

正在尝试找到ListBoxItem的祖先(ListBoxItem 型(。当然,除非你们在彼此内部嵌套ListBoxItems否则这并不存在。

将其更改为:

 <Trigger Property="IsMouseOver" Value="True">
    <!-- ... -->
 </Trigger>

此外,您正在覆盖ListBoxItem的模板,并且在您的自定义模板中没有任何指向 ListBoxItem.Background 属性的内容,该画笔永远不会在屏幕上可见。

我建议你把它用于StackPanel的背景:

<ControlTemplate TargetType="{x:Type ListBoxItem}">
    <StackPanel Orientation="Horizontal" Height="50" Background="{TemplateBinding Background}">
       <!-- ... -->
    </StackPanel>
</ControlTemplate>

另外,我认为您可能会将ListBoxItem.Template的概念与ListBox.ItemTemplate混淆,后者是用于在常规ListBoxItems中呈现Data DataTemplate。我建议您查看本教程以获取更多信息。