WPF中ListView如何改變選中條背景顏色

來源:互聯網
上載者:User

標籤:

先

解決方案:

<ListView ...>    <ListView.ItemContainerStyle>        <Style TargetType="{x:Type ListViewItem}">            <Style.Resources>                <!-- Foreground for Selected ListViewItem -->                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"                                  Color="Black"/>                <!-- Background for Selected ListViewItem -->                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"                                 Color="Transparent"/>            </Style.Resources>        </Style>    </ListView.ItemContainerStyle>    ...</ListView>

 

解決問題

在CSDN上找到另外一種方法:

用StyleSnooper看一下預設的Style,改一下就可以了。三種顏色 IsMouseOver = Blue        Select & Focus = Red        Select & UnFocus = Yellow

大家也可以嘗試下:

<Window x:Class="WpfApplication1.Window1"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    Title="Window1" Height="300" Width="300">    <Window.Resources>        <Style TargetType="ListViewItem">            <Style.Resources>                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Yellow"/>                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color=" Red"/>            </Style.Resources>            <Setter Property="Panel.Background" Value="#00FFFFFF"/>            <Setter Property="Control.HorizontalContentAlignment">                <Setter.Value>                    <Binding Path="HorizontalContentAlignment"                              RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}" />                </Setter.Value>            </Setter>            <Setter Property="Control.VerticalContentAlignment">                <Setter.Value>                    <Binding Path="VerticalContentAlignment"                              RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}" />                </Setter.Value>            </Setter>            <Setter Property="Control.Padding" Value="2,0,0,0"/>            <Setter Property="Control.Template">                <Setter.Value>                    <ControlTemplate TargetType="ListViewItem">                        <Border BorderThickness="{TemplateBinding Border.BorderThickness}"                                 Padding="{TemplateBinding Control.Padding}"                                 BorderBrush="{TemplateBinding Border.BorderBrush}"                                 Background="{TemplateBinding Panel.Background}"                                 Name="Bd"                                 SnapsToDevicePixels="True">                            <ContentPresenter                                 Content="{TemplateBinding ContentControl.Content}"                                 ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"                                 ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"                                 HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"                                 VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"                                 SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />                        </Border>                        <ControlTemplate.Triggers>                            <Trigger Property="Selector.IsSelected" Value="True">                                <Setter Property="Panel.Background" TargetName="Bd">                                    <Setter.Value>                                        <DynamicResource ResourceKey="{x:Static SystemColors.HighlightBrushKey}" />                                    </Setter.Value>                                </Setter>                                <Setter Property="TextElement.Foreground">                                    <Setter.Value>                                        <DynamicResource ResourceKey="{x:Static SystemColors.HighlightTextBrushKey}" />                                    </Setter.Value>                                </Setter>                            </Trigger>                            <MultiTrigger>                                <MultiTrigger.Conditions>                                    <Condition Property="Selector.IsSelected" Value="True"/>                                    <Condition Property="Selector.IsSelectionActive" Value="False"/>                                </MultiTrigger.Conditions>                                <Setter Property="Panel.Background" TargetName="Bd">                                    <Setter.Value>                                        <DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" />                                    </Setter.Value>                                </Setter>                                <Setter Property="TextElement.Foreground">                                    <Setter.Value>                                        <DynamicResource ResourceKey="{x:Static SystemColors.ControlTextBrushKey}" />                                    </Setter.Value>                                </Setter>                            </MultiTrigger>                            <Trigger Property="UIElement.IsEnabled" Value="False">                                <Setter Property="TextElement.Foreground">                                    <Setter.Value>                                        <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />                                    </Setter.Value>                                </Setter>                            </Trigger>                            <Trigger Property="UIElement.IsMouseOver" Value="True">                                <Setter Property="Panel.Background" TargetName="Bd" Value="Blue">                                </Setter>                            </Trigger>                        </ControlTemplate.Triggers>                    </ControlTemplate>                </Setter.Value>            </Setter>        </Style>    </Window.Resources>    <Grid>        <ListView Margin="48,22,110,0" Name="listView1" Height="100" VerticalAlignment="Top">            <ListView.View>                <GridView>                    <GridViewColumn Header="12"/>                    <GridViewColumn Header="12"/>                    <GridViewColumn Header="12"/>                </GridView>            </ListView.View>            <ListViewItem>123</ListViewItem>            <ListViewItem>123</ListViewItem>            <ListViewItem>123</ListViewItem>        </ListView>        <TextBox Height="23" Margin="94,0,64,67" Name="textBox1" VerticalAlignment="Bottom" />    </Grid></Window>

 

 

var isLogined = false; var cb_blogId = 69072; var cb_entryId = 3335531; var cb_blogApp = currentBlogApp; var cb_blogUserGuid = "c846af3f-b66b-de11-9510-001cf0cd104b"; var cb_entryCreatedDate = ‘2013/9/23 18:50:00‘; var enableGoogleAd = true; var googletag = googletag || {}; googletag.cmd = googletag.cmd || [];

WPF中ListView如何改變選中條背景顏色

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.