Metro App ListView SelectedItem Selected VisualState [英] Metro App ListView SelectedItem Selected VisualState

查看:24
本文介绍了Metro App ListView SelectedItem Selected VisualState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Metro 应用,其 ListView 包含以下定义:

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>

ListView 选择了一个项目时,我想更改 tb1tb2Foreground ONLYWhite.我该怎么做?

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?

我尝试为 Selected 覆盖 Themed BrushesVisualStateGroup SelectionStates ,但没有帮助.一个有效的代码示例将不胜感激.

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

推荐答案

终于RESOLVED这个,感谢这篇文章给我的想法:

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

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

创建了一个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;
    }
}

在 App.xaml 中添加了这个

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

然后像这样使用它:

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

这篇关于Metro App ListView SelectedItem Selected VisualState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆