如何更改ListView中TextBlock中的选定文本颜色?

时间:2013-03-12 13:49:14

标签: c# xaml windows-8 visual-studio-2012 .net-4.5

我正在构建Windows 8商店应用程序,我无法在ListView中更改TextBlock的突出显示/选定颜色a。

以下是我创建ListView的方法:

<ListView x:Name="zonesList" HorizontalAlignment="Left" Height="516" Margin="50,175,0,0" ItemTemplate="{StaticResource LocalizationCellSample}" VerticalAlignment="Top" Width="355" DataContext="{Binding}" SelectionChanged="zonesList_SelectionChanged"/>

我的数据绑定:

<DataTemplate x:Key="LocalizationCellSample">
  <Grid HorizontalAlignment="Left">
    <TextBlock Style="{StaticResource ListBoxTextStyle}" TextWrapping="Wrap" Width="200" MaxHeight="20" VerticalAlignment="Center" Text="{Binding name}" HorizontalAlignment="Left" Margin="20,5,0,0" FontFamily="Global User Interface"/>
  </Grid>
</DataTemplate>

最简单的方法是什么?

编辑:

这是ListBoxTextStyle

<Style x:Key="ListBoxTextStyle" TargetType="TextBlock">
  <Setter Property="FontFamily" Value="Segoe UI Light" />
  <Setter Property="FontSize" Value="16" />
</Style>

3 个答案:

答案 0 :(得分:1)

Add ItemContainerStyle to the ListView

添加ItemContainerStyle,如#34; DocumentOutline&#34; Visual Studio中的面板。然后编辑VisualState,如下所示:

<VisualState x:Name="Selected">
    <Storyboard>
        <ColorAnimation Duration="0" To="Red" Storyboard.TargetProperty="(ContentPresenter.Foreground).(SolidColorBrush.Color)"
                        Storyboard.TargetName="contentPresenter" />
    </Storyboard>
</VisualState>
<VisualState x:Name="SelectedUnfocused">
    <Storyboard>
        <ColorAnimation Duration="0" To="Red" Storyboard.TargetProperty="(ContentPresenter.Foreground).(SolidColorBrush.Color)" 
                        Storyboard.TargetName="contentPresenter" />
    </Storyboard>
</VisualState>

通常,"contentPresenter"默认会添加到ItemContainerStyleForeground更改"contentPresenter",文本颜色会发生变化。

至于所选Background的{​​{1}},与上述几乎相同。

顺便说一句,它至少在ListViewItem中起作用。如果你在WP8.1中这样做,那将会更容易。

答案 1 :(得分:0)

您可以尝试将其添加到Common文件夹中的standardstyles.xaml文件中:

<SolidColorBrush x:Key="ListViewItemSelectedBackgroundThemeBrush" Color="#009ACD"></SolidColorBrush>
        <SolidColorBrush x:Key="ListViewItemPointerOverBackgroundThemeBrush" Color="#CCC"></SolidColorBrush>
        <SolidColorBrush x:Key="ListViewItemSelectedPointerOverBackgroundThemeBrush" Color="#00688B"></SolidColorBrush>
        <SolidColorBrush x:Key="ListViewItemSelectedPointerOverBorderThemeBrush" Color="Black" Opacity="0"></SolidColorBrush>

在您的resourcedictionary标记内

所以看起来像这样:

<ResourceDictionary.ThemeDictionaries>
    <ResourceDictionary x:Key="Default">

        <SolidColorBrush x:Key="ListViewItemSelectedBackgroundThemeBrush" Color="#009ACD"></SolidColorBrush>
        <SolidColorBrush x:Key="ListViewItemPointerOverBackgroundThemeBrush" Color="#CCC"></SolidColorBrush>
        <SolidColorBrush x:Key="ListViewItemSelectedPointerOverBackgroundThemeBrush" Color="#00688B"></SolidColorBrush>
        <SolidColorBrush x:Key="ListViewItemSelectedPointerOverBorderThemeBrush" Color="Black" Opacity="0"></SolidColorBrush>


    </ResourceDictionary>


</ResourceDictionary.ThemeDictionaries>

这样,列表视图中的所有项目在被选中时都会获得某种颜色

答案 2 :(得分:-1)

在StandardStyles.xaml文件中添加下面提到的代码并更改所需的颜色:

   <SolidColorBrush x:Key="ListViewItemSelectedForegroundThemeBrush" Color="Red" />