0

リストボックスのアイテムスタイルとトリガーを使用してマウスオーバーの色を変更していますが、スタイルが機能していないようです:

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

エラーは理解していますが、スタイルを理解するのに助けが必要です-データテンプレートで試してみましたが、うまくいきませんでした。

4

1 に答える 1