简体   繁体   English

WPF - 如何更改 ListBox 中的文本和图像颜色?

[英]WPF - How to change text and image color in ListBox?

I want to make a menu from a ListBox How can I change the text and image color when an item is selected?我想从 ListBox 制作一个菜单 如何在选择项目时更改文本和图像颜色?

Example:例子:

enter image description here在此处输入图像描述

I got it like this:我是这样得到的:

enter image description here在此处输入图像描述

Code XAML:代码 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>

To change the Text color you could simply put another Setter into your "IsSelected"-Trigger like:要更改文本颜色,您可以简单地将另一个 Setter 放入您的“IsSelected”-Trigger 中,例如:

<Setter Property="Foreground" Value="White"/>

Changing the color of the.png-Image wont be that simple though.改变.png-Image 的颜色并不那么简单。 You should consider converting your Icons (like the home.png) into Paths, so you can transform them easier and change their color ("Fill"-Property)您应该考虑将您的图标(如 home.png)转换为路径,这样您就可以更轻松地转换它们并更改它们的颜色(“填充”-属性)

If you want to go further with your project, also consider putting your styles into seperate ResourceDictionaries to clean up your code如果您想进一步处理您的项目 go,还可以考虑将您的 styles 放入单独的 ResourceDictionaries 中以清理您的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM