本文介绍了ListViewItem 样式不能触发 Unselected的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ListViewItem 样式不能触发 Unselected,但可以触发 Selected.

The ListViewItem style cant trigger Unselected but can trigger Selected.

我想看到在选择 listView 项时显示的内容,以及在未选择时隐藏的内容.

I want to see someThing showed when the listView item be selected and hid when unselected.

但是我可以看到它被显示了,但看不到它被隐藏了.

But I can see that it was shown, but could not see it being hidden.

我从 https://msdn 复制样式.microsoft.com/en-us/library/windows/apps/mt299136.aspx

我确定我将 SelectionMode 设置为 Single.

And I sure I set the SelectionMode is Single.

我复制的代码是

  <Style x:Key="ListViewItemStyle" TargetType="ListViewItem">
            <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
            <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
            <Setter Property="Background" Value="{ThemeResource ListViewItemBackground}"/>
            <Setter Property="Foreground" Value="{ThemeResource ListViewItemForeground}"/>
            <Setter Property="TabNavigation" Value="Local"/>
            <Setter Property="IsHoldingEnabled" Value="True"/>
            <Setter Property="Padding" Value="12,0,12,0"/>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="MinWidth" Value="{ThemeResource ListViewItemMinWidth}"/>
            <Setter Property="MinHeight" Value="{ThemeResource ListViewItemMinHeight}"/>
            <Setter Property="AllowDrop" Value="False"/>
            <Setter Property="UseSystemFocusVisuals" Value="True"/>
            <Setter Property="FocusVisualMargin" Value="0"/>
            <Setter Property="FocusVisualPrimaryBrush" Value="{ThemeResource ListViewItemFocusVisualPrimaryBrush}"/>
            <Setter Property="FocusVisualPrimaryThickness" Value="2"/>
            <Setter Property="FocusVisualSecondaryBrush" Value="{ThemeResource ListViewItemFocusVisualSecondaryBrush}"/>
            <Setter Property="FocusVisualSecondaryThickness" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListViewItem">
                        <Grid>
                            <ContentPresenter ></ContentPresenter>
                            <Button x:Name="b" Opacity="0"></Button>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="SelectionStates">
                                    <VisualState x:Name="Unselecting">
                                        <Storyboard BeginTime="0:0:0">
                                            <DoubleAnimation Storyboard.TargetName="b"
                                                     Storyboard.TargetProperty="Opacity"
                                                     Duration="0:0:0.1"
                                                     To="0" />
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Unselected">
                                        <Storyboard BeginTime="0:0:0">
                                            <DoubleAnimation Storyboard.TargetName="b"
                                                     Storyboard.TargetProperty="Opacity"
                                                     Duration="0"
                                                     To="0" />
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Selected">
                                        <Storyboard BeginTime="0:0:0">
                                            <DoubleAnimation Storyboard.TargetName="b"
                                                     Storyboard.TargetProperty="Opacity"
                                                     Duration="0"
                                                     To="1" />
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>

            </Setter>

        </Style>

我已经看到这个答案 https://stackoverflow.com/a/36010296/6116637 Archana 提供了一种方法使用 CustomUnselected 并在 SelectionChanged 时更改它.

I have seen this answer https://stackoverflow.com/a/36010296/6116637 that Archana offered a way that use CustomUnselected and change it when SelectionChanged.

这是一个好方法,但我不想将相同的代码写到很多地方,因为我有很多 ListView 应该这样做.有没有什么好的方法可以只写 xaml?

It's a good way but I dont want to write the same code to lot of place for I have many ListView should do it.Are there any good way to do it only write xaml?

我已经看到说绑定到 IsSelected 的答案,但是当我想要 TemplateBinding 的 Visibility 不能使用 convert 时,TemplateBinding 不能这样做.

I have seen the answer that say binding to IsSelected but the TemplateBinding cant do it when I want to Visibility for the TemplateBinding cant use convert.

当我使用 Visibility="{Binding Path={TemplateBinding IsSelected},Converter={StaticResource BooleanVisibility},Mode=OneWay}"Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected,Converter={StaticResource BooleanInverseVisibility},Mode=OneWay}" ,结果不是我想要的.

And when I use the Visibility="{Binding Path={TemplateBinding IsSelected},Converter={StaticResource BooleanVisibility},Mode=OneWay}" or Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected,Converter={StaticResource BooleanInverseVisibility},Mode=OneWay}" , the result is not what I want.

请参阅:https://stackoverflow.com/a/40328520/6116637

推荐答案

创建逆可见性转换器:

public class BooleanInverseVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return (bool)value ? Visibility.Collapsed : Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return (Visibility)value != Visibility.Visible;
    }
}

正确时折叠,错误时显示.

This collapses when it's true and shows when it's false.

Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected,Converter={StaticResource BooleanInverseVisibility},Mode=OneWay}"

这篇关于ListViewItem 样式不能触发 Unselected的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 14:43