首页 文章

在metro UI应用程序中更改所选ListViewItem的文本颜色

提问于
浏览
0

请帮我把事情搞清楚 .

我正在使用包含ListView的Windows 8 metro应用程序 . 我的listview包含TextBlocks . 像这样的东西:

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颜色,因此在Designer模式下,我选择了“编辑其他模板/编辑生成的项目容器”,并在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的颜色应为黑色 . 如果未选择项目 - 白色 .

AND HERE IS A QUESTION: 我应该在哪里放置用于处理TextBlock颜色的逻辑?如果在StandardStyles.xml中的某处,那么我将如何将其分配给TextBlock?如果在列表视图项模板中的某处,那么我应该如何获得选择状态?

非常感谢您的帮助!谢谢 .

1 回答

  • 1

    EDITED:

    尝试将这些动画添加到 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>
    

相关问题