1

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

Community
  • 1
  • 1
lindexi
  • 4,182
  • 3
  • 19
  • 65
  • I also see https://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj709921.aspx?f=255&MSPPError=-2147217396 – lindexi Apr 18 '17 at 01:10
  • See http://stackoverflow.com/questions/40327901/how-to-bind-isselected-property-in-datatemplate-of-listviewitem I dont want to bind it to IsSelected. – lindexi Apr 18 '17 at 07:52
  • Can you post the datatemplate code? Can you tell what you want to show and what not? – Archana Apr 20 '17 at 04:28
  • I want to bind to Data in ViewModel that have some show when selected. – lindexi Apr 20 '17 at 05:06
  • Do you want to do binding in template or datatemplate? Can you post Full ListView code? – Archana Apr 20 '17 at 13:57
  • Yes ,but it's a demo.If you have another code that can bind to vm and show when seleted,it's what I want. – lindexi Apr 21 '17 at 01:09
  • Did you follow this http://stackoverflow.com/questions/40327901/how-to-bind-isselected-property-in-datatemplate-of-listviewitem? i think this would solve your problem. You can have property in vm and bind that property to visibility of the control which you want to toggle. On selectionchanged event make that property value to true. – Archana Apr 21 '17 at 05:10

1 Answers1

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}"
Laith
  • 6,071
  • 1
  • 33
  • 60
  • Just realized I copy/pasted your code which was incorrect. Please look at the updated `{Binding ..}` sample. – Laith Apr 18 '17 at 01:44
  • the code is lose the convert that is like you support.Thx your reply but your code cant work too.I know that the RelativeSource can binding to TemplatedParent but it not I want.See http://stackoverflow.com/questions/40327901/how-to-bind-isselected-property-in-datatemplate-of-listviewitem – lindexi Apr 18 '17 at 07:52