【问题标题】:Visualstate Unselected not calledVisualstate Unselected 未调用
【发布时间】:2016-05-13 16:20:43
【问题描述】:

我在 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,但没有帮助。 我该怎么做才能取消选择该项目?

【问题讨论】:

  • 如果我没记错的话,我认为 SelectionMode 用于 ListBox 而 MultiSelect="False" 将用于 ListView。
  • @ChrisW:我尝试设置 MultiSelect,但 Windows Universal 中 ListView 的属性不再存在。我尝试了所有的选择模式,但没有用。
  • 你解决了吗?目前正在拉扯我的头发-_-

标签: xaml win-universal-app


【解决方案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);
            }
        }

【讨论】:

    【解决方案2】:

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

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

    不再支持“SelectionStates”VisualStatesGroup 中的所有 VisualStates。我设法通过在 ControlTemplate 类中使用 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>

    希望对您有所帮助! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-03
      • 2017-09-13
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多