【问题标题】:Metro App Change ListView Selected Item Content ForegroundMetro App 更改 ListView 所选项目内容前景
【发布时间】:2013-04-12 17:47:44
【问题描述】:

我有一个ListView,并将它的DataTemplate 修改为2 个TextBlocks

第一个TextBlock 包含一个标题,第二个包含一个子标题

我为 2 TextBlocks 设计了不同的颜色。

以下是 普通 视图中ListViewItem 的示例。

下面是 Selected 视图中ListViewItem 的示例。

所以我的问题是如何在 Selected 视图中更改TextBlocksForeground 颜色?希望在 xaml 中做到这一点。我尝试设置不同的画笔,它们适用于未明确设置样式的项目。

不确定如何处理这种情况。

【问题讨论】:

标签: xaml listview microsoft-metro winrt-xaml


【解决方案1】:

您可以使用视觉状态。

<ListView>
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <TextBlock x:Name="txtOne" Grid.Row="0" Foreground="Green"/>
                <TextBlock x:Name="txtTwo" Grid.Row="1" Foreground="Gray"/>
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="SelectionStates">
                        <VisualState x:Name="Unselected"/>
                        <VisualState x:Name="Selected">
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="txtOne" Storyboard.TargetProperty="Foreground">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="Red"/>
                                </ObjectAnimationUsingKeyFrames>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="txtTwo" Storyboard.TargetProperty="Foreground">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="Yellow"/>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

【讨论】:

    【解决方案2】:

    你不需要玩视觉状态。

    在您的 ResourceDictionary 中,为这些画笔设置一个值“ListBoxItemSelectedBackgroundThemeBrush”、“ListBoxItemSelectedPointerOverBackgroundThemeBrush”、“ListBoxFocusBackgroundThemeBrush”。它将覆盖您应用程序的默认画笔。

    例子:

        <!-- Overrides default ListBox brushes -->
    <SolidColorBrush x:Key="ListBoxItemSelectedBackgroundThemeBrush" Color="{StaticResource GreenColor}" />
    <SolidColorBrush x:Key="ListBoxItemSelectedPointerOverBackgroundThemeBrush" Color="{StaticResource LightGreenColor}" />
    <SolidColorBrush x:Key="ListBoxFocusBackgroundThemeBrush" Color="Transparent" />
    

    这是一个在WinRt中开发时有用的链接,它引用了画笔名称,用于winRt的默认控件。

    WinRt default brushes names and values

    【讨论】:

    • 这个方案不行,c0D3l0g1c想改变datatemplate中textblocks的前景。
    【解决方案3】:

    感谢一些开箱即用的研究和思考,找到了一个合适的解决方案:

    Metro App ListView SelectedItem Selected VisualState

    我可以看到这对于其他几个场景也很方便。

    【讨论】:

      猜你喜欢
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-21
      • 1970-01-01
      相关资源
      最近更新 更多