Wpf 如何更改列表框中SelectedItem的前景?
我昨天问了这个问题,得到了很好的快速回答。但现在我有了一些不同的东西,最后一个解决方案不起作用 首先让我们看看我的xaml:Wpf 如何更改列表框中SelectedItem的前景?,wpf,xaml,listbox,Wpf,Xaml,Listbox,我昨天问了这个问题,得到了很好的快速回答。但现在我有了一些不同的东西,最后一个解决方案不起作用 首先让我们看看我的xaml: <ListBox ItemsSource="{Binding Children}" x:Name="lst"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <WrapPanel IsItemsHost="True" Orientation="
<ListBox ItemsSource="{Binding Children}" x:Name="lst">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Vertical" MaxHeight="{Binding ElementName=lst, Path=ActualHeight}"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Width" Value="250" />
<Setter Property="Height" Value="125" />
<Setter Property="Margin" Value="2.5" />
<Setter Property="Padding" Value="2.5" />
<Setter Property="Background" Value="{Binding Background, Converter={StaticResource stringToBrushConverter}}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="VerticalContentAlignment" Value="Bottom" />
</Style>
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="125" Width="250">
<Path Data="{Binding Image}" VerticalAlignment="Center"
Stretch="Uniform" Fill="#FFFFFFFF"
Width="68" Height="68" Margin="10" RenderTransformOrigin="0.5,0.5">
<Path.RenderTransform>
<TransformGroup>
<TransformGroup.Children>
<RotateTransform Angle="0" />
<ScaleTransform ScaleX="1" ScaleY="1" />
</TransformGroup.Children>
</TransformGroup>
</Path.RenderTransform>
</Path>
<TextBlock Text="{Binding Title, Converter={StaticResource spaceToNewLineConverter}}" VerticalAlignment="Top"
Margin="40,10,10,10" FontSize="24" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
上面的xaml工作得非常好,并提供了良好的输出。但现在我想更改SelectedItem的前景色
你可以找到我昨天问的问题
昨天问题的答案是这样的:
<Style x:Key="BlankListBoxContainerStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="FocusVisualStyle" Value="{x:Null} "/>
</Style>
如果我如上面代码所示更改了模板,我就会丢失我在列表框上应用的DataTemplate
我还尝试了
solidcolorbush
和使用设置器和触发器
。但是我运气不好。在这种情况下,您需要传递ListBoxItem
的数据模板中的前台
值。这可以通过以下方式完成:
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal"
Height="125"
Width="250">
...
<TextBlock Foreground="{Binding Path=Foreground,
RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" ... />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
...
您可以在自己的风格中使用触发器来实现这一点。试着这样做:
<ListBox.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Width" Value="250" />
<Setter Property="Height" Value="125" />
<Setter Property="Margin" Value="2.5" />
<Setter Property="Padding" Value="2.5" />
<Setter Property="Background" Value="{Binding Background, Converter={StaticResource stringToBrushConverter}}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="VerticalContentAlignment" Value="Bottom" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Resources>
不,我认为您误解了我的问题,或者可能是我误解了您。如果我不使用BasedOn,则ListBox.Resources部分中声明的样式可以与样式BlankListBoxContainerStyle配合使用。但问题是,如果我将BlankListBoxContainerStyle与ListBox.ItemTemplate一起使用,则无法获得正确的输出。是的,我误解了这个问题。现在我明白了,当您需要一个模板时,您正在尝试设置这两个模板。在您的数据模板中
尝试此构造可能会有所帮助:
。是的,这解决了问题。谢谢你再次解决了我的问题。