【发布时间】:2014-09-22 04:41:03
【问题描述】:
我对 ListBoxItem 样式和触发器有疑问。 我创建了一个测试项目。在列表中我有项目。我为这个 ListBox 创建了样式和触发器。当我将鼠标悬停在 IsMouseOver 项目上时,触发器工作并且将 Margin、FontSize、Cursor、Foreground 但不将 Background 和 TesxDecoretions 变为下划线。这是测试项目代码。
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;
谢谢!
【问题讨论】: