Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListBoxItem IsSelected style

I still didn't get it. Could you please show me exactly how to override ListBox's default behavior. Everytime when ListBoxItem is selected the Border's background should be changed. Not the background of the whole row but only background of the border which's specified.

 <ListBox ItemsSource="{Binding Source={StaticResource AssetsViewSource}}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderThickness="2" BorderBrush="Black">
                    <StackPanel>
                        <TextBlock Text="Name: " />
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
like image 960
iLemming Avatar asked Oct 18 '25 18:10

iLemming


1 Answers

Use the DataTemplate's Triggers collection, with a RelativeSource to get you to the containing ListBoxItem:

<DataTemplate>
  <Border BorderThickness="2" BorderBrush="Black" Name="Bd">
    <StackPanel>
      <TextBlock Text="Name: " />
      <TextBlock Text="{Binding Name}" />
    </StackPanel>
  </Border>
  <DataTemplate.Triggers>
    <DataTrigger Value="True"
                 Binding="{Binding 
                              IsSelected, 
                              RelativeSource={RelativeSource 
                                  AncestorType={x:Type ListBoxItem}}}">
      <!-- everybody loves HotPink -->
      <Setter TargetName="Bd" Property="Background" Value="HotPink"/>  
    </DataTrigger>
  </DataTemplate.Triggers>
</DataTemplate>
like image 134
itowlson Avatar answered Oct 21 '25 00:10

itowlson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!