3

我还是没明白。你能否告诉我如何覆盖 ListBox 的默认行为。每次选择 ListBoxItem 时,都应更改边框的背景。不是整行的背景,而是指定的边框的背景。

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

2 回答 2

11

使用 DataTemplate 的 Triggers 集合和一个 RelativeSource 让您进入包含的 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>
于 2010-04-07T00:00:51.130 回答
2

只需将以下内容添加到 ListBox Item 标记中

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

这应该够了吧..

于 2010-08-16T15:43:13.617 回答