C# ListViewItem样式不能触发未选中

C# ListViewItem样式不能触发未选中,c#,uwp,win-universal-app,C#,Uwp,Win Universal App,ListViewItem样式不能触发未选定项,但可以触发选定项 我想看到listView项目被选中时显示的内容,以及取消选中时隐藏的内容 但我可以看到它被显示出来,但看不到它被隐藏 我从中复制样式 我确定我把SelectionMode设置为Single 我从中复制的代码是 <Style x:Key="ListViewItemStyle" TargetType="ListViewItem"> <Setter Property="FontFamily"

ListViewItem样式不能触发未选定项,但可以触发选定项

我想看到listView项目被选中时显示的内容,以及取消选中时隐藏的内容

但我可以看到它被显示出来,但看不到它被隐藏

我从中复制样式

我确定我把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>

我已经看到了这个答案,Archana提供了一种使用CustomUnselected并在SelectionChange时更改它的方法

这是一个很好的方法,但我不想在很多地方写同样的代码,因为我有很多ListView应该做这件事。有没有好的方法只写xaml

我已经看到了答案,说绑定到IsSelected,但是TemplateBinding不能在我想查看TemplateBinding时执行,因为TemplateBinding不能使用convert

当我使用
Visibility=“{Binding Path={TemplateBinding IsSelected},Converter={StaticResource BooleanVisibility},Mode=OneWay}”或
Visibility=“{Binding RelativeSource={RelativeSource TemplatedParent},Path=IsSelected,Converter={StaticResource booleanversevisibility},Mode=OneWay}”时,
,结果不是我想要的


请参见:

创建反向可见性转换器:

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

我也看到我不想把它绑定到IsSelected。你能发布数据模板代码吗?您能告诉我您想显示什么和不想显示什么吗?我想绑定到ViewModel中的数据,这些数据在选中时有一些显示。您想在template还是datatemplate中进行绑定?你能发布完整的ListView代码吗?刚刚意识到我复制/粘贴了你的代码,这是不正确的。请查看更新的
{Binding..}
示例。代码丢失了您支持的转换。谢谢您的回复,但您的代码也无法工作。我知道RelativeSource可以绑定到TemplatedParent,但它不是我想要的。请参阅