ListViewItem样式无法触发Unselected但可以触发Selected。
我希望看到someThing在选择listView项目时显示,并在未选中时隐藏。
但是我可以看到它已经显示出来了,但却看不到它被隐藏了。
我从https://msdn.microsoft.com/en-us/library/windows/apps/mt299136.aspx
复制样式我确定我将SelectionMode设置为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时更改它。
这是一个很好的方法,但我不想将相同的代码编写到很多地方因为我有很多ListView应该这样做。有没有什么好的方法只能写xaml?
我已经看到了答案,即绑定到IsSelected但是当我想要TemplateBinding的可见性时,TemplateBinding无法使用转换。
当我使用Visibility="{Binding Path={TemplateBinding IsSelected},Converter={StaticResource BooleanVisibility},Mode=OneWay}"
或Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected,Converter={StaticResource BooleanInverseVisibility},Mode=OneWay}"
时,结果不是我想要的。
答案 0 :(得分:1)
创建逆可见性转换器:
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;
}
}
如果它是真的就会崩溃并显示它的错误。
Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected,Converter={StaticResource BooleanInverseVisibility},Mode=OneWay}"