Listview有多项选择甚至设置选择模式单一?
Listview has multiple selection even setting selection mode single?
我有一个列表视图,但我的列表视图有多个选择,即使选择模式是单一的?我已经发布了在列表视图和 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="" 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>
Listview has multiple selection even setting selection mode single
这是因为您缺少Nomal
视觉状态。您在 Listviewitem 样式中设置了选定的视觉状态,但没有给出 Nomal
状态。 ListViewItem 中的未选中状态是 Nomal
状态。因此,在您的 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>
我有一个列表视图,但我的列表视图有多个选择,即使选择模式是单一的?我已经发布了在列表视图和 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="" 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>
Listview has multiple selection even setting selection mode single
这是因为您缺少Nomal
视觉状态。您在 Listviewitem 样式中设置了选定的视觉状态,但没有给出 Nomal
状态。 ListViewItem 中的未选中状态是 Nomal
状态。因此,在您的 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>