Visualstate Unselected 未调用

Visualstate Unselected not called

我在 Windows 通用项目中有一个 ListView 对象。我想为 Selected 和 Unselected 状态设置两个 Visual States。我从 MSDN 复制了代码:

            <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListViewItem">
                    <Border>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal">

                                </VisualState>
                                <VisualState x:Name="Disabled"/>
                                <VisualState x:Name="MouseOver">

                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected">
                                    <Storyboard>
                                        <ColorAnimation Duration="0:0:.1" To="{StaticResource HighlightColor}" Storyboard.TargetName="brd" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                            <ColorAnimation.EasingFunction>
                                                <PowerEase Power="2" />
                                            </ColorAnimation.EasingFunction>
                                        </ColorAnimation>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ColorAnimation Duration="0:0:.1" To="{StaticResource FlowBlue}" Storyboard.TargetName="brd" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                            <ColorAnimation.EasingFunction>
                                                <PowerEase Power="2" />
                                            </ColorAnimation.EasingFunction>
                                        </ColorAnimation>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border Tapped="brd_Tapped" GotFocus="brd_GotFocus" Width="100" Height="85" Background="{StaticResource HighlightColorBrush}" BorderThickness="0" Name="brd" Tag=""  Margin="5">
                            <Grid Name="grd" Tag="">
                                <TextBlock FontWeight="Bold" VerticalAlignment="Bottom" FontSize="18" HorizontalAlignment="Right" Foreground="{ThemeResource ApplicationPageBackgroundThemeBrush}" />
                                <control:TextBoxEx LostFocus="TextBoxEx_LostFocus" IsHitTestVisible="True" DoubleTapped="TextBoxEx_DoubleTapped" Background="Transparent" Text="{Binding Path=ItemName, UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap" Width="100" />
                            </Grid>
                        </Border>
                    </Border>
                </ControlTemplate>
            </Setter.Value>

当我 运行 并单击一个项目时,将调用 Selected 状态。但是当我点击另一个项目时,它也会设置 Selected 状态。但第一项不会取消选择。我尝试将 SelectionMode 设置为 Single 但它没有帮助。 我该怎么做才能取消选择该项目?

作为为将 Win 8.1 应用程序迁移到 UWP 应用程序而提供的文档的一部分,MSDN 此处记录了 GridView 上的重大更改。

https://msdn.microsoft.com/en-us/library/windows/apps/mt188204.aspx#gridview

不再支持 'SelectionStates' VisualStatesGroup 中的所有 VisualStates。我设法通过在 ControlTemplate class 中使用 ListViewItemPresenter 来解决这个问题。

请参考此处建议的默认样式:https://msdn.microsoft.com/en-us/library/windows/apps/mt299127.aspx

您的 GridView.xaml 应如下所示;

<!-- Default style for Windows.UI.Xaml.Controls.GridViewItem -->
<Style TargetType="GridViewItem">
  <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
  <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
  <Setter Property="Background" Value="Transparent"/>
  <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
  <Setter Property="TabNavigation" Value="Local"/>
  <Setter Property="IsHoldingEnabled" Value="True"/>
  <Setter Property="HorizontalContentAlignment" Value="Center"/>
  <Setter Property="VerticalContentAlignment" Value="Center"/>
  <Setter Property="Margin" Value="0,0,4,4"/>
  <Setter Property="MinWidth" Value="{ThemeResource GridViewItemMinWidth}"/>
  <Setter Property="MinHeight" Value="{ThemeResource GridViewItemMinHeight}"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="GridViewItem">
        <ListViewItemPresenter
            ContentTransitions="{TemplateBinding ContentTransitions}"
            SelectionCheckMarkVisualEnabled="True"
            CheckBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
            CheckBoxBrush="{ThemeResource SystemControlBackgroundChromeMediumBrush}"
            DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}"
            DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}"
            FocusBorderBrush="{ThemeResource SystemControlForegroundAltHighBrush}"
            FocusSecondaryBorderBrush="{ThemeResource SystemControlForegroundBaseHighBrush}"
            PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}"
            PointerOverBackground="{ThemeResource SystemControlHighlightListLowBrush}"
            PointerOverForeground="{ThemeResource SystemControlForegroundBaseHighBrush}"
            SelectedBackground="{ThemeResource SystemControlHighlightAccentBrush}"
            SelectedForeground="{ThemeResource SystemControlForegroundBaseHighBrush}"
            SelectedPointerOverBackground="{ThemeResource SystemControlHighlightListAccentMediumBrush}"
            PressedBackground="{ThemeResource SystemControlHighlightListMediumBrush}"
            SelectedPressedBackground="{ThemeResource SystemControlHighlightListAccentHighBrush}"
            DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
            DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}"
            ReorderHintOffset="{ThemeResource GridViewItemReorderHintThemeOffset}"
            HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
            VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
            ContentMargin="{TemplateBinding Padding}"
            CheckMode="Overlay"/>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

希望对您有所帮助! :)

首先在代码behind.Try中为列表视图项目挂钩选择更改事件behind.Try选择中的这段代码已更改event.Hope它可能对您有所帮助。

      if (e.AddedItems != null)
    {
        foreach (var item in e.AddedItems)
        {
            ListViewItem litem = (sender as ListView).ContainerFromItem(item) as ListViewItem;
            if (litem != null)
            {
                VisualStateManager.GoToState(litem, "Unfocused", true);
                VisualStateManager.GoToState(litem, "Normal", true);
                VisualStateManager.GoToState(litem, "Selected", true);
            }
        }
    }
    if (e.RemovedItems != null)
    {
        foreach (var item in e.RemovedItems)
        {
            ListViewItem litem = (sender as ListView).ContainerFromItem(item) as ListViewItem;
            if (litem != null)
            {
                VisualStateManager.GoToState(litem, "Unselected", true);
            }
        }