简体   繁体   English

如何更改ListView / GridView中选定行的文本颜色? (使用表达黑暗主题)

[英]How do I change text color on the selected row inside a ListView/GridView? (using Expression Dark theme)

I'm using theExpression Dark WPF Theme( http://wpfthemes.codeplex.com/ ) with a ListView(view property set to a GridView) to display some user data like the following : 我正在将Expression Dark WPF主题( http://wpfthemes.codeplex.com/ )与ListView(将view属性设置为GridView)一起使用,以显示一些用户数据,如下所示:

<ListView
      Grid.Row="1"
      ItemsSource="{Binding RegisteredUsers}"
      SelectedItem="{Binding SelectedUser}"
      > 
      <ListView.View>
        <GridView>
          <GridViewColumn            
            Header="Login"
            DisplayMemberBinding="{Binding Login}"
            Width="60"/>
          <GridViewColumn
            Header="Full Name"
            DisplayMemberBinding="{Binding FullName}"
            Width="180"/>
          <GridViewColumn
            Header="Last logon"
            DisplayMemberBinding="{Binding LastLogon}"
            Width="120"/>
          <GridViewColumn
            Header="Photo"
            Width="50">
            <GridViewColumn.CellTemplate>
              <DataTemplate>
                <Image 
                  Source="{Binding Photo}" 
                  Width="30" 
                  Height="35"/>
              </DataTemplate>
            </GridViewColumn.CellTemplate>
          </GridViewColumn>
        </GridView>
      </ListView.View>
    </ListView>

The rows have white text with a dark background and white background when selected, however the text color doesnt change when selected and it makes very difficult to read, I would like the text to have a dark color when the row is selected. 这些行具有白色文本,带有深色背景,并且在选择时具有白色背景,但是在选择时,文本颜色不会改变并且很难阅读。我希望在选择该行时,文本具有深色。 I have searched for a way to style the text color but with no success, here is the control template for the ListViewItem : 我一直在寻找一种样式设置文本颜色的方法,但是没有成功,这是ListViewItem的控件模板:

<Border SnapsToDevicePixels="true" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2" x:Name="border">

            <Grid Margin="2,0,2,0">
              <Rectangle x:Name="Background" IsHitTestVisible="False" Opacity="0.25" Fill="{StaticResource NormalBrush}" RadiusX="1" RadiusY="1"/>
              <Rectangle x:Name="HoverRectangle" IsHitTestVisible="False" Opacity="0" Fill="{StaticResource NormalBrush}" RadiusX="1" RadiusY="1"/>
              <Rectangle x:Name="SelectedRectangle" IsHitTestVisible="False" Opacity="0" Fill="{StaticResource SelectedBackgroundBrush}" RadiusX="1" RadiusY="1"/>
              <GridViewRowPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="0,2,0,2" VerticalAlignment="Stretch" />
            </Grid>

</Border>

The trigger that changes the background color simply applies an animation to change the 'SelectedRectangle' opacity, but I cant change the text color on the same trigger(I tried using a setter for the foreground color on the ListViewItem, but with no success). 更改背景颜色的触发器只是应用动画来更改“ SelectedRectangle”的不透明度,但我无法在同一触发器上更改文本颜色(我尝试使用Setter作为ListViewItem的前景色,但没有成功)。

Does someone have a clue on that? 有人对此有线索吗?

尝试更改TextBlock.Foreground

 <GridViewRowPresenter TextBlock.Foreground="{TemplateBinding Foreground}"/>

Managed to solve by applying a textblock style on the GridViewRowPresenter scope : 通过在GridViewRowPresenter范围内应用文本块样式来解决:

<GridViewRowPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="0,2,0,2" VerticalAlignment="Stretch" >
                                <GridViewRowPresenter.Resources>
                                    <Style        
                                        TargetType="{x:Type TextBlock}"
                                        BasedOn="{StaticResource {x:Type TextBlock}}"
                                        >      
                                        <Style.Triggers>
                                            <DataTrigger
                                                Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}}"
                                                Value="True">
                                                <Setter 
                                                    Property="Foreground"
                                                    Value="Black"
                                                    />
                                            </DataTrigger>
                                        </Style.Triggers>                                                                             
                                    </Style>
                                </GridViewRowPresenter.Resources>
                            </GridViewRowPresenter>

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

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