【问题标题】:Changing text color for selected ListViewItem in metro UI application在 Metro UI 应用程序中更改选定 ListViewItem 的文本颜色
【发布时间】:2013-06-15 20:58:14
【问题描述】:

如何把东西放在一起?

我正在开发包含 ListView 的 Windows 8 Metro 应用程序。我的列表视图包含文本块。 像这样的:

MyPage.xaml:

<DataTemplate x:Key="ListViewItemTemplate">
     <StackPanel>
          <TextBlock Text="{Binding Goal, Mode=OneWay}"/>
     </StackPanel>
 </DataTemplate>

<ListView x:Name="ChainsList" 
          ItemsSource="{Binding Chains}" 
          SelectedItem="{Binding Path=SelectedChain, Mode=TwoWay}"
          ItemTemplate="{StaticResource ListViewItemTemplate}" 
          ItemContainerStyle="{StaticResource ChainsListViewItemStyle}">
</ListView>

我不喜欢选中/取消选中项目的默认 ListView 颜色,因此在设计器模式下,我选择了“编辑其他模板/编辑生成的项目容器”并在 StandardStyles.xml 中创建了自己的 ListViewItem 样式副本:

<Style x:Key="ChainsListViewItemStyle" TargetType="ListViewItem">
    <!-- a lot of setters goes here -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListViewItem">
                <Border x:Name="OuterContainer">
                    <!-- description of visual states goes here (I changed some colors) -->
                    <Grid x:Name="ReorderHintContent" Background="Transparent">
                       <!-- List view item structure details goes here -->
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

现在我想根据选择更改列表视图项的文本颜色。如果项目选择了 TextBlock 的颜色应该是黑色的。如果未选择项目 - 白色。

这里有一个问题:我应该把改变 TextBlock 颜色的逻辑放在哪里?如果在 StandardStyles.xml 中的某个地方,那么我将如何将它分配给 TextBlock?如果在列表视图项模板中的某个位置,那么我应该如何获得选择状态?

【问题讨论】:

    标签: xaml listview colors windows-runtime microsoft-metro


    【解决方案1】:

    已编辑:

    尝试以ChainsListViewItemStyle 样式将这些动画添加到SelectionStates VisualStateGroup

    <VisualState x:Name="Unselected">
        <Storyboard>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="contentPresenter">
                <DiscreteObjectKeyFrame KeyTime="0" Value="White"/>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </VisualState>
    <VisualState x:Name="Selected">
        <Storyboard>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="contentPresenter">
                <DiscreteObjectKeyFrame KeyTime="0" Value="Black"/>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </VisualState>
    <VisualState x:Name="SelectedSwiping">
        <Storyboard>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="contentPresenter">
                <DiscreteObjectKeyFrame KeyTime="0" Value="Black"/>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </VisualState>
    <VisualState x:Name="SelectedUnfocused">
        <Storyboard>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="contentPresenter">
                <DiscreteObjectKeyFrame KeyTime="0" Value="Black"/>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </VisualState>
    

    【讨论】:

    • 很遗憾,Winrt 中没有触发器。
    • 嗯,我学到了一些新东西!在这种情况下,您将需要使用 VisualStateManager。我会更新我的答案。
    • Richard,在我最初的帖子中,我为 ChainsListViewItemStyle 放置了样式。在文本中有一条评论: 所以我已经玩过视觉状态并改变了颜色。但是我看不到如何从视觉状态中引用 DataTemplate 中描述的 TextBlock 。您建议的方法行不通,因为实际上我有几个 TextBlocks,它们应该是不同的颜色。改变 ContentPresenters 的属性是不够的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    相关资源
    最近更新 更多