我有此列表框样式,但由于某些原因,我无法更改基于触发器的前景颜色。当列表框中的项目被选中时,我想将前景色更改为黑色。我到处寻找,所有的解决方案似乎都不适用于我的情况。这是我的列表框。
<ListBox IsSynchronizedWithCurrentItem="True" SelectedValuePath="RecordId" ItemsSource="{Binding People}" SelectedValue="{Binding SelectedPersonId}" HorizontalAlignment="Stretch" Margin="20,5,10,5" Width="Auto" Grid.Row="1" Background="{x:Null}" BorderBrush="{x:Null}" x:Name="lstCustomers">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="Auto" Width="Auto" d:DesignWidth="545.375" d:DesignHeight="50.294">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="132.375"/>
</Grid.ColumnDefinitions>
<StackPanel x:Name="stackPanel" Orientation="Horizontal" HorizontalAlignment="Stretch">
<Label FontSize="16" Content="{Binding FullName}" Foreground="White"/>
<Label FontSize="16" Content="{Binding DisplayName}" Margin="5,0,0,0" Foreground="White"/>
</StackPanel>
<DockPanel LastChildFill="True" HorizontalAlignment="Stretch" Margin="0" Grid.Column="1">
<Label Content="{Binding Date}" ContentStringFormat="yyyy/MM/dd" FontSize="24" HorizontalAlignment="Right" Foreground="White" DockPanel.Dock="Right" VerticalAlignment="Center"/>
</DockPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border BorderBrush="#26FFFFFF" BorderThickness="3" CornerRadius="5" Name="Border" Margin="0,0,2,3" Padding="0" SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="#7FFFFFFF" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border" Property="Background" Value="#0FFFFFFF"></Setter>
<Setter TargetName="Border" Property="BorderBrush" Value="#BEFFFFFF"></Setter>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelected" Value="true"></Condition>
<Condition Property="IsMouseOver" Value="true"></Condition>
</MultiTrigger.Conditions>
<Setter TargetName="Border" Property="Background" Value="#7FFFFFFF"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>任何帮助都将不胜感激!
谢谢!
发布于 2012-02-10 01:42:04
您的触发器工作正常,但是我没有看到任何指定当选择一个项目时前景应为黑色的内容...
<ControlTemplate TargetType="ListBoxItem">
<Border BorderBrush="#26FFFFFF" BorderThickness="3" CornerRadius="5" Name="Border" Margin="0,0,2,3" Padding="0" SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="#7FFFFFFF" />
<Setter TargetName="Border" Property="Foreground" Value="Black" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border" Property="Background" Value="#0FFFFFFF"></Setter>
<Setter TargetName="Border" Property="BorderBrush" Value="#BEFFFFFF"></Setter>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelected" Value="true"></Condition>
<Condition Property="IsMouseOver" Value="true"></Condition>
</MultiTrigger.Conditions>
<Setter TargetName="Border" Property="Background" Value="#7FFFFFFF"/>
<Setter TargetName="Border" Property="Foreground" Value="Black" />
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>但是,我确实看到过将Label的Foreground颜色设置为白色的代码。这将覆盖您在ListBoxItem样式中设置的任何前景色。
我建议将默认前景色添加到ListBoxItem样式中,并将Label前景色绑定到ListBoxItem前景色,或者使用TextBlock而不是Label,后者将默认继承前景色。
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Foreground" Value="White" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Template">
...
</Setter>
</Style>和
<Label Text="Name" Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=Foreground}"/>或
<TextBlock Text="Name" />https://stackoverflow.com/questions/9215630
复制相似问题