Metro App ListView SelectedItem选择VisualState

时间:2013-06-09 14:22:29

标签: windows-runtime microsoft-metro winrt-xaml .net-4.5

我有 Metro App ListView包含此定义:

        <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选中某个项目时,我想将Foreground的{​​{1}}和tb1 更改为{{1} }。我该怎么做呢?

我尝试覆盖主题笔刷tb2 White替换VisualStateGroup,但没有帮助。一个工作代码示例将不胜感激。

1 个答案:

答案 0 :(得分:4)

最后已解决这一点,感谢这篇文章给了我这个想法:

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}}"