Visualstate Unselected未调用

时间:2015-05-06 10:48:57

标签: xaml win-universal-app

我在Windows Universal Project中有一个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但它没有帮助。 如何取消选择该项目?

2 个答案:

答案 0 :(得分:1)

首先为后面的代码中的列表视图项挂钩选择更改事件。在选择更改事件中尝试此代码。希望它可以帮助您。

      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);
            }
        }

答案 1 :(得分:0)

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

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

&#39; SelectionStates&#39;中的所有视觉状态。不再支持VisualStatesGroup。我设法通过在ControlTemplate类中使用ListViewItemPresenter来解决这个问题。

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

你的GridView.xaml应该是这样的;

&#13;
&#13;
<!-- 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>
&#13;
&#13;
&#13;

希望它有所帮助! :)