2014-09-22 4 views
0

У меня есть вопрос о стилизации и триггере ListBoxItem. Я создал один тестовый проект. В списке у меня есть элементы. и я создал стиль и триггер для этого ListBox. Когда я нажимаю на объект, срабатывает триггер IsMouseOver и создает границы, FontSize, Cursor, Foreground, но не подчеркивает Background и TesxDecoretions, чтобы подчеркнуть. Вот код тестовых проектов.WPF: ListBoxItem background not chenging

XAML

<Window x:Class="TestForJamshed.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 
    <Style TargetType="ListBox" x:Key="PanelPreviewShortListBox"> 
     <Setter Property="BorderThickness" Value="0"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate> 
        <Grid Background="{TemplateBinding Background}"> 
         <ItemsPresenter Margin="-10 0 0 0"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Setter Property="ItemContainerStyle"> 
      <Setter.Value> 
       <Style TargetType="ListBoxItem"> 
        <Setter Property="Margin" Value="10 0 0 0"/> 
        <Setter Property="BorderThickness" Value="0"/> 

        <Style.Triggers> 
         <Trigger Property="IsSelected" Value="True"> 
          <Setter Property="Margin" Value="-10 0 0 0"/> 
          <Setter Property="TextBlock.FontSize" Value="15"/> 
          <Setter Property="Cursor" Value="Hand"/> 
         </Trigger> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="Margin" Value="-10 0 0 0"/> 
          <Setter Property="Background" Value="Red"/> 
          <Setter Property="TextBlock.FontSize" Value="15"/> 
          <Setter Property="Cursor" Value="Hand"/> 
          <Setter Property="Foreground" Value="#0000FF"/> 
          <Setter Property="TextBlock.TextDecorations" Value="Underline"/> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 
<Grid> 
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"> 
     <ListBox Name="uiListBox" HorizontalAlignment="Right" Style="{DynamicResource PanelPreviewShortListBox}" Width="200" Background="LightGreen"/> 
    </StackPanel> 
</Grid> 

КОД

List<string> list = new List<string>(); 
     list.Add("This is an item."); 
     list.Add("This is an item."); 
     list.Add("This is an item."); 
     list.Add("This is an item."); 
     list.Add("This is an item."); 
     list.Add("This is an item."); 
     list.Add("This is an item."); 

     uiListBox.ItemsSource = list; 

Thanx!

ответ

1

TargetType был ListBox, и я сменил его на ListBoxItem, и он боится!