The ListViewItem style cant trigger Unselected

2019-07-28 21:33发布

问题:

The ListViewItem style cant trigger Unselected but can trigger Selected.

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.

I copy the style from https://msdn.microsoft.com/en-us/library/windows/apps/mt299136.aspx

And I sure I set the SelectionMode is Single.

The code that I copy from is

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

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

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?

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.

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.

See:https://stackoverflow.com/a/40328520/6116637

回答1:

Create an inverse visibility converter:

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