Metro App Change ListView Selected Item Content Foreground

China☆狼群 提交于 2019-12-05 06:38:59

问题


I have a ListView and modified it's DataTemplate with 2 TextBlocks.

The first TextBlock contains a Heading, the second a Sub-Heading.

I style the 2 TextBlocks with different colours.

Here's an example of the ListViewItem in Normal view.

Here's an example of the ListViewItem in Selected view.

So my question is how do I change the Foreground colours of the TextBlocks in Selected views? Hoping to do this in the xaml. I've tried setting different brushes, which work for items that haven't explicitly been styled.

Not sure how to handle this scenario.


回答1:


You can use visual states.

<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:


You don't need to play with the visual state.

In your ResourceDictionary, set a value for these brushes "ListBoxItemSelectedBackgroundThemeBrush", "ListBoxItemSelectedPointerOverBackgroundThemeBrush", "ListBoxFocusBackgroundThemeBrush". It will override the default brushes of your application.

Example:

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

Here is a usefull link when developping in WinRt, which references the brushes name, for the default controls of winRt.

WinRt default brushes names and values




回答3:


Thanks to some researching and thinking out of the box, found a suitable solution that works:

Metro App ListView SelectedItem Selected VisualState

I can see this being handy for a couple of other scenarios as well.



来源:https://stackoverflow.com/questions/15977737/metro-app-change-listview-selected-item-content-foreground

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!