我有多个共享样式的TextBlocks列表框。我有3个问题:
以下是XAML:
<ListBox Grid.Row="1" Grid.Column="0" ItemsSource="{Binding ProductionLists.Raw}" Style="{StaticResource MyListBoxStyle}"/>
<ListBox Grid.Row="1" Grid.Column="1" ItemsSource="{Binding ProductionLists.Tier1}" Style="{StaticResource MyListBoxStyle}"/>
<Style x:Key="MyListBoxStyle" TargetType="ListBox">
<Setter Property="Background" Value="LightGray"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding Path=ItemName, Mode=OneWay}"
Style="{StaticResource ListBoxItemStyle}"
PreviewMouseUp="ShowProductionMap">
</TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ListBoxItemStyle" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsAvailable}" Value="False">
<Setter Property="Foreground" Value="DarkGray"/>
</DataTrigger>
</Style.Triggers>
<Setter Property="Background" Value="LightGray"/>
</Style>
找出#2,将listboxitem水平内容对齐设置为拉伸。
而且,如果项IsAvailable(根据XAML设置),我能够设置所选项的文本颜色,但如果不是IsAvailable,则它不会着色。
因此,对于正在选择该项目而不是IsAvailable的情况,我希望仍能解决#1,而在ListBox外部选择一个项目时所选项目保持白色的情况则要解决#3。
XAML:
<Style x:Key="ListBoxItemStyle" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsAvailable}" Value="False">
<Setter Property="Foreground" Value="DarkGray"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="MyListBoxStyle" TargetType="ListBox">
<Setter Property="Background" Value="LightGray"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="ListBoxItem.IsSelected" Value="True">
<Setter Property="Foreground" Value="IndianRed"/>
</Trigger>
</Style.Triggers>
</Style>
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding Path=ItemName, Mode=OneWay}"
Style="{StaticResource ListBoxItemStyle}"
PreviewMouseUp="ShowProductionMap"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>