开发者

ListBoxItem IsSelected style

开发者 https://www.devze.com 2022-12-26 02:15 出处:网络
I still didn\'t get it. Could you please show me exactly how to override ListBox\'s default behavior.

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>
                开发者_JAVA技巧<Border BorderThickness="2" BorderBrush="Black">
                    <StackPanel>
                        <TextBlock Text="Name: " />
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>


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>


Simply add the following into the ListBox Item tag

<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
</ListBox.Resources>

That should do the trick..

0

精彩评论

暂无评论...
验证码 换一张
取 消