none
如何修改Listview选中项的颜色 RRS feed

  • 问题

  • 我在listview 这样写好像不行啊

     <ListView.ItemContainerStyle>
                        <Style TargetType="ListBoxItem" >
                            <Setter Property="Background" Value="White" />
                            <Style.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter Property="Background" Value="White" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>

                    </ListView.ItemContainerStyle>

    2011年12月31日 3:52

答案

  • 你好,

    你的代码没有什么问题,可以成功覆盖默认的trigger, 把选中时的背景色改成白色。

    因为你两个Setter的值都是白色,看上去你想在选中时不改变颜色?这样的话,默认模板中的高亮效果你这样是没法改的,你需要重写Template才行。例如,

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <Border BorderThickness="{TemplateBinding BorderThickness}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                Background="{TemplateBinding Background}"
                                CornerRadius="2"
                                SnapsToDevicePixels="true">
                            <Border Name="InnerBorder"
                                    CornerRadius="1"
                                    BorderThickness="1">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition MaxHeight="11"/>
                                        <RowDefinition/>
                                    </Grid.RowDefinitions>
                                    <Rectangle Name="UpperHighlight"
                                               Visibility="Collapsed"
                                               Fill="#75FFFFFF"/>
                                    <GridViewRowPresenter Grid.RowSpan="2"
                                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Grid>
                            </Border>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver"
                                     Value="true">
                                <Setter Property="Background"
                                        Value="{StaticResource ListItemHoverFill}"/>
                                <Setter Property="BorderBrush"
                                        Value="#FFCCF0FF"/>
                                <Setter TargetName="UpperHighlight"
                                        Property="Visibility"
                                        Value="Visible"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
    


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    2012年1月2日 1:24
    版主

全部回复

  • <Style x:Key="ItemContStyle" TargetType="{x:Type ListViewItem}">
      <Style.Resources>
        <!-- Brushes omitted for clarity� -->
        <!-- Reduces a customer's Balance to either -1, 0, or +1 -->
        <local:NumberToPolarValueConverter x:Key="PolarValueConv" />
      </Style.Resources>
    
      <!-- Stretch the content so that we can right-align values
           in the Balance column. -->
      <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    
      <Style.Triggers>
        <!-- When a customer owes money, color them green. -->
        <DataTrigger
          Binding="{Binding Balance, Converter={StaticResource PolarValueConv}}"
          Value="+1"
          >
          <Setter Property="Background" Value="{StaticResource ProfitBrush}" />
        </DataTrigger>
    
        <!-- When a customer is owed money, color them red. -->
        <DataTrigger
          Binding="{Binding Balance, Converter={StaticResource PolarValueConv}}"
          Value="-1"
          >
          <Setter Property="Background" Value="{StaticResource LossBrush}" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
    http://www.codeproject.com/KB/WPF/HiliteListViewItemsInWPF.aspx
    • 已编辑 KKLEO 2012年1月1日 7:29
    2012年1月1日 7:28
  • 你好,

    你的代码没有什么问题,可以成功覆盖默认的trigger, 把选中时的背景色改成白色。

    因为你两个Setter的值都是白色,看上去你想在选中时不改变颜色?这样的话,默认模板中的高亮效果你这样是没法改的,你需要重写Template才行。例如,

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <Border BorderThickness="{TemplateBinding BorderThickness}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                Background="{TemplateBinding Background}"
                                CornerRadius="2"
                                SnapsToDevicePixels="true">
                            <Border Name="InnerBorder"
                                    CornerRadius="1"
                                    BorderThickness="1">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition MaxHeight="11"/>
                                        <RowDefinition/>
                                    </Grid.RowDefinitions>
                                    <Rectangle Name="UpperHighlight"
                                               Visibility="Collapsed"
                                               Fill="#75FFFFFF"/>
                                    <GridViewRowPresenter Grid.RowSpan="2"
                                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Grid>
                            </Border>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver"
                                     Value="true">
                                <Setter Property="Background"
                                        Value="{StaticResource ListItemHoverFill}"/>
                                <Setter Property="BorderBrush"
                                        Value="#FFCCF0FF"/>
                                <Setter TargetName="UpperHighlight"
                                        Property="Visibility"
                                        Value="Visible"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
    


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    2012年1月2日 1:24
    版主