我有这种列表框样式但由于某种原因我无法根据触发器更改前景色。我想在选择列表框中的项目时将前景色更改为黑色。我搜遍了所有的解决方案似乎在我的情况下不起作用。这是我的列表框......
<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>
任何帮助将不胜感激!
谢谢!
答案 0 :(得分:2)
你的触发器工作正常,但是当我选择一个项目时,我没有看到任何指定前景应该是黑色的东西....
<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>
我确实看到了将Foreground
的{{1}}颜色设置为白色的代码。这将覆盖您在Label
样式中设置的任何前景色。
我建议为您的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}"/>