English 中文(简体)
名单BoxItem IsS
原标题: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>
最佳回答

利用数据Template s Triggers Collection, with a RelativeSource to receive You to the contained 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>
问题回答

简单地将以下内容列入清单Box项目标签

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

这应当做到。





相关问题
Creating a Style in code behind

Does anyone know how to create a wpf Style in code behind, I can t find anything on the web or MSDN docs. I have tried this but it is not working: Style s = new Style(typeof(TextBlock)); s....

WPF Custom Themes

I have a simple question which is giving me some difficulty. I have downloaded a custom them for WPF of the net. Now i want to apply this theme to my App instead of the default one. How do i do that,...

Is it sometimes bad to use <BR />?

Is it sometimes bad to use <BR/> tags? I ask because some of the first advice my development team gave me was this: Don t use <BR/> ; instead, use styles. But why? Are there negative ...

WPF - How to apply effect to a cropped image?

I have an Image being clipped like so: <Image Width="45" Grid.Column="0" Source="{Binding Photo}"> <Image.Clip> <RectangleGeometry Rect="0,0,45,55" RadiusX="8" RadiusY="8" /...

WPF ListView : Header styling

I want to have a ListView with columns and a particular style: The background for ALL column headers should be transparent except when the mouse is over in one of them. When this happends, the ...

热门标签