WPF - 如何更改 ListBox 中的文本和图像颜色?
WPF - How to change text and image color in ListBox?
我想从 ListBox 制作一个菜单
选择项目时如何更改文本和图像颜色?
示例:
enter image description here
我是这样理解的:
enter image description here
代码XAML:
<ListView Background="Transparent"
BorderBrush="Transparent"
HorizontalAlignment="Left"
Margin="0 0 0 0">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border Name="_Border" Margin="3 0 3 0" Padding="5 5 5 5"
CornerRadius="3"
SnapsToDevicePixels="true"
Background="Transparent">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="_Border" Property="Background" Value="#36A3F6"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListViewItem IsSelected="True">
<StackPanel Orientation="Horizontal">
<Image Source="Images/home.png" Width="15" Height="15" Margin="5 0 5 0"/>
<TextBlock Text="item1"/>
</StackPanel>
</ListViewItem>
<ListViewItem>
<StackPanel Orientation="Horizontal">
<Image Source="Images/home.png" Width="15" Height="15" Margin="5 0 5 0"/>
<TextBlock Text="item2"/>
</StackPanel>
</ListViewItem>
<ListViewItem>
<StackPanel Orientation="Horizontal">
<Image Source="Images/home.png" Width="15" Height="15" Margin="5 0 5 0"/>
<TextBlock Text="item3"/>
</StackPanel>
</ListViewItem>
</ListView>
要更改文本颜色,您只需将另一个 Setter 放入您的“IsSelected”触发器中,例如:
<Setter Property="Foreground" Value="White"/>
更改 .png-Image 的颜色并不那么简单。
您应该考虑将您的图标(如 home.png)转换为路径,这样您就可以更轻松地转换它们并更改它们的颜色(“填充”-属性)
如果您想进一步推进您的项目,还可以考虑将您的样式放入单独的 ResourceDictionaries 中以清理您的代码
我想从 ListBox 制作一个菜单 选择项目时如何更改文本和图像颜色?
示例:
enter image description here
我是这样理解的:
enter image description here
代码XAML:
<ListView Background="Transparent"
BorderBrush="Transparent"
HorizontalAlignment="Left"
Margin="0 0 0 0">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border Name="_Border" Margin="3 0 3 0" Padding="5 5 5 5"
CornerRadius="3"
SnapsToDevicePixels="true"
Background="Transparent">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="_Border" Property="Background" Value="#36A3F6"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListViewItem IsSelected="True">
<StackPanel Orientation="Horizontal">
<Image Source="Images/home.png" Width="15" Height="15" Margin="5 0 5 0"/>
<TextBlock Text="item1"/>
</StackPanel>
</ListViewItem>
<ListViewItem>
<StackPanel Orientation="Horizontal">
<Image Source="Images/home.png" Width="15" Height="15" Margin="5 0 5 0"/>
<TextBlock Text="item2"/>
</StackPanel>
</ListViewItem>
<ListViewItem>
<StackPanel Orientation="Horizontal">
<Image Source="Images/home.png" Width="15" Height="15" Margin="5 0 5 0"/>
<TextBlock Text="item3"/>
</StackPanel>
</ListViewItem>
</ListView>
要更改文本颜色,您只需将另一个 Setter 放入您的“IsSelected”触发器中,例如:
<Setter Property="Foreground" Value="White"/>
更改 .png-Image 的颜色并不那么简单。 您应该考虑将您的图标(如 home.png)转换为路径,这样您就可以更轻松地转换它们并更改它们的颜色(“填充”-属性)
如果您想进一步推进您的项目,还可以考虑将您的样式放入单独的 ResourceDictionaries 中以清理您的代码