Вопрос

I am using a listbox itemstyle and triggers to change the mouseover color but the style does not seem to work:

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

I get this error in the output window:

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

I understand that the error but need help trying figure out the style - I have tried doing it with a datatemplate and that did not work out either.

Это было полезно?

Решение

Your DataTrigger:

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

Is trying to find the Ancestor (of Type ListBoxItem) of the ListBoxItem. Of course that doesn't exist unless you nest ListBoxItems inside of each other.

Change it to this:

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

Also, you're overriding the ListBoxItem's Template, and in your custom template there's nothing pointing to the ListBoxItem.Background property, that brush will never be visible on screen.

I suggest you use it for the StackPanel's Background:

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

Also, I think you might be confusing the concept of ListBoxItem.Template with ListBox.ItemTemplate, which is a DataTemplate used to render your Data inside regular ListBoxItems. I suggest you take a look at this tutorial for more info.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top