Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
839 views
in Technique[技术] by (71.8m points)

windows runtime - Metro App ListView SelectedItem Selected VisualState

I have a Metro App with a ListView that contains this definition:

        <ListView Grid.Row="0" x:Name="lv" CanDragItems="True" CanReorderItems="True" IsTabStop="True" SelectionMode="Extended" VirtualizingStackPanel.VirtualizationMode="Recycling">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Width="{Binding ElementName=lv, Path=ActualWidth}">
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>

                        <Grid Grid.Row="0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="65"/>
                            </Grid.ColumnDefinitions>

                            <TextBlock Grid.Column="0" x:Name="tb1" Foreground="{StaticResource SecondaryColourBrush}" HorizontalAlignment="Stretch"/>

                            <TextBlock Grid.Column="4" x:Name="tb2" Foreground="{StaticResource SecondaryColourBrush}" HorizontalAlignment="Right"/>

                        </Grid>

                        <Grid Grid.Row="1">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="65"/>
                            </Grid.ColumnDefinitions>

                            <TextBlock Grid.Column="0" x:Name="tb3" Foreground="{StaticResource QuadColourBrush}" HorizontalAlignment="Stretch" TextTrimming="WordEllipsis"/>

                            <TextBlock Grid.Column="1" x:Name="tb4" Foreground="{StaticResource QuadColourBrush}" HorizontalAlignment="Right"/>

                        </Grid>

                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

When the ListView has an item selected, I want to change the Foreground of tb1 and tb2 ONLY to White. How do I go about doing this?

I tried overriding Themed Brushes and VisualStateGroup SelectionStates for Selected, which hasn't helped. A working code example would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Finally RESOLVED this, thanks to this article for giving me the idea:

http://blog.davemdavis.net/2012/11/12/controlling-the-datatemplate/

Created a BooleanToColourConverter

public sealed class BooleanToColourConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return (value is bool && (bool)value) ? AppResources.TertiaryColourBrush : AppResources.PrimaryColourBrush;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return value is SolidColorBrush && ((SolidColorBrush)value).Color == AppResources.TertiaryColourBrush.Color;
    }
}

Added this in App.xaml

<common:BooleanToColourConverter x:Key="BooleanToColourConverter"/>

Then used it like this:

Foreground="{Binding Tag, Converter={StaticResource BooleanToColourConverter}, Mode=TwoWay, RelativeSource={RelativeSource Mode=TemplatedParent}}"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...