XAML 列表视图有多个选择,即使设置选择模式单一?

vu8f3i0k  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(106)

我有一个列表视图,但我的列表视图有多个选择,即使选择模式是单一的?我已经张贴的属性设置列表视图和itemcontainerstyle。

<ListView SelectionMode="Single" Tapped="UIElementTapped"
                    ItemsSource="{Binding Path=ListTimeFrame}"
                    SelectedItem="{Binding Path=SelectedTimeFrame, Mode=TwoWay}"
                    ItemContainerStyle="{StaticResource TimeFrameListViewItemContainerStyle}"
                    Padding="0"
                    Margin="0">

和itemcontainer样式,如下所示

<Style x:Key="TimeFrameListViewItemContainerStyle" TargetType="ListViewItem">
    <Setter Property="Padding" Value="4,1" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderBrush" Value="Transparent" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListViewItem">
                <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"
                       >
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="SelectionStates">
                            <VisualState x:Name="Unselected" />
                            <VisualState x:Name="Selected">
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="SelectingGlyph"
                                            Storyboard.TargetProperty="Opacity" Duration="0" To="1" />
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentPresenter"
                                            Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0"
                                                Value="{StaticResource DefaultTextBlueColor}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="SelectedUnfocused" />
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Grid Margin="{TemplateBinding Padding}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="&#xE10B;" FontFamily="Segoe UI Symbol"
                                Foreground="{StaticResource DefaultTextBlueColor}" x:Name="SelectingGlyph"
                                Opacity="0" FontSize="20" LineStackingStrategy="BlockLineHeight" />
                        <ContentPresenter x:Name="contentPresenter" Grid.Column="1"
                                ContentTransitions="{TemplateBinding ContentTransitions}"
                                ContentTemplate="{TemplateBinding ContentTemplate}"
                                Content="{TemplateBinding Content}"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
jgwigjjp

jgwigjjp1#

列表视图有多个选择,即使设置选择模式单一
这是因为您缺少Normal视觉状态。您在ListViewItem样式中设置了选定的视觉状态,但没有给予Normal状态。ListViewItem中的未选定状态是Normal状态。因此,在SelectionStates视觉状态组中添加以下视觉状态。

<VisualState x:Name="Normal">
    <Storyboard>
        <PointerUpThemeAnimation Storyboard.TargetName="contentPresenter" />
    </Storyboard>
</VisualState>

所有可视状态代码如下所示:

<VisualStateManager.VisualStateGroups>
     <VisualStateGroup x:Name="SelectionStates">
         <VisualState x:Name="Normal">
             <Storyboard>
                 <PointerUpThemeAnimation Storyboard.TargetName="contentPresenter" />
             </Storyboard>
         </VisualState>        
         <VisualState x:Name="Selected">
             <Storyboard>
                 <DoubleAnimation Duration="0"
                                  Storyboard.TargetName="SelectingGlyph"
                                  Storyboard.TargetProperty="Opacity"
                                  To="1" />
                 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="Foreground">
                     <DiscreteObjectKeyFrame KeyTime="0" Value="Blue" />
                 </ObjectAnimationUsingKeyFrames>
             </Storyboard>
         </VisualState>
         <VisualState x:Name="SelectedUnfocused" />
     </VisualStateGroup>
 </VisualStateManager.VisualStateGroups>

相关问题