简体   繁体   English

UWP ComboBoxItem是否为“未选中” VisualState?

[英]“Unselected” VisualState for UWP ComboBoxItem?

Is there an equivalent for the "Unselected" VisualState in UWP ComboBox? UWP ComboBox中的“未选定” VisualState是否等效? We are migrating from Windows 8 and would like a solution that is cross-compatible between each other. 我们正在从Windows 8迁移,希望有一个相互交叉兼容的解决方案。

I looked at the Default Styles for ComboBoxItem and UWP does not seem to have a VisualState for " Unselected ". 我查看了ComboBoxItem的默认样式,UWP似乎没有“ 未选中 ”的VisualState。

Here is a screen shot of what happens: 这是发生的情况的屏幕截图:

在此处输入图片说明

The Blue one is the items that have been selected. 蓝色的是已选择的项目。 Since there is no " Unselected " Visual State in UWP, it does not unselect the previously selected items. 由于UWP中没有“ 选定的”可视状态,因此不会取消选定先前选定的项目。

Here is my code for the ComboBoxItem Style: 这是我的ComboBoxItem样式代码:

<Style x:Key="Style_ComboBox_CheckSelector_ItemContainer" TargetType="ComboBoxItem">
        <Setter Property="TabNavigation" Value="Local" />
        <Setter Property="Padding" Value="8,10" />
        <Setter Property="HorizontalContentAlignment" Value="Left" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBoxItem">
                    <Border x:Name="LayoutRoot"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="0:0:0" />
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="PointerOver">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0"
                                                     Storyboard.TargetName="Rectangle_Background_OverState"
                                                     Storyboard.TargetProperty="Opacity"
                                                     To="1" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0"
                                                     Storyboard.TargetName="Rectangle_DisabledVisualElement"
                                                     Storyboard.TargetProperty="Opacity"
                                                     To="1" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0"
                                                     Storyboard.TargetName="Rectangle_PressedVisualElement"
                                                     Storyboard.TargetProperty="Opacity"
                                                     To="1" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="0:0:0" />
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Unselected" />
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0"
                                                     Storyboard.TargetName="ContentPresenter_Selected"
                                                     Storyboard.TargetProperty="Opacity"
                                                     To="1" />
                                        <DoubleAnimation Duration="0"
                                                     Storyboard.TargetName="Rectangle_FocusVisualElement"
                                                     Storyboard.TargetProperty="Opacity"
                                                     To="1" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="SelectedUnfocused">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0"
                                                     Storyboard.TargetName="ContentPresenter_Selected"
                                                     Storyboard.TargetProperty="Opacity"
                                                     To="1" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="SelectedDisabled">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0"
                                                     Storyboard.TargetName="ContentPresenter_Selected"
                                                     Storyboard.TargetProperty="Opacity"
                                                     To="1" />
                                        <DoubleAnimation Duration="0"
                                                     Storyboard.TargetName="Rectangle_DisabledVisualElement"
                                                     Storyboard.TargetProperty="Opacity"
                                                     To="1" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="SelectedPointerOver">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0"
                                                     Storyboard.TargetName="ContentPresenter_Selected"
                                                     Storyboard.TargetProperty="Opacity"
                                                     To="1" />
                                        <DoubleAnimation Duration="0"
                                                     Storyboard.TargetName="Rectangle_Background_OverState"
                                                     Storyboard.TargetProperty="Opacity"
                                                     To="1" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="SelectedPressed">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0"
                                                     Storyboard.TargetName="ContentPresenter_Selected"
                                                     Storyboard.TargetProperty="Opacity"
                                                     To="1" />
                                        <DoubleAnimation Duration="0"
                                                     Storyboard.TargetName="Rectangle_PressedVisualElement"
                                                     Storyboard.TargetProperty="Opacity"
                                                     To="1" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="0:0:0" />
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Focused">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0"
                                                     Storyboard.TargetName="Rectangle_FocusVisualElement"
                                                     Storyboard.TargetProperty="Opacity"
                                                     To="1" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unfocused" />
                                <VisualState x:Name="PointerFocused" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid x:Name="InnerGrid">
                            <Rectangle x:Name="Rectangle_Background_NormalState" Fill="#FF404040" />
                            <Rectangle x:Name="Rectangle_Background_OverState"
                                   Fill="#FF666666"
                                   Opacity="0"
                                   Stroke="#FF999999"
                                   StrokeThickness="1" />
                            <ContentPresenter x:Name="ContentPresenter"
                                          Margin="{TemplateBinding Padding}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          Content="{TemplateBinding Content}"
                                          ContentTemplate="{TemplateBinding ContentTemplate}"
                                          ContentTransitions="{TemplateBinding ContentTransitions}"
                                          Foreground="#FFCCCCCC" />
                            <ContentPresenter x:Name="ContentPresenter_Selected"
                                          Margin="{TemplateBinding Padding}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          Content="{TemplateBinding Content}"
                                          ContentTemplate="{TemplateBinding ContentTemplate}"
                                          ContentTransitions="{TemplateBinding ContentTransitions}"
                                          Foreground="Blue"
                                          Opacity="0" />
                            <Grid x:Name="Grid_ControlOverlayParts" IsHitTestVisible="False">
                                <Rectangle x:Name="Rectangle_PressedVisualElement"
                                       Fill="#33FFFFFF"
                                       Opacity="0" />
                                <Rectangle x:Name="Rectangle_FocusVisualElement"
                                       Opacity="0" />
                                <Rectangle x:Name="Rectangle_DisabledVisualElement"
                                       Fill="#7F1A1A1A"
                                       Opacity="0" />
                            </Grid>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Thank you! 谢谢!

I do not recommend sharing a template for the same control across Windows 8/8.1 and UWP. 我不建议在Windows 8 / 8.1和UWP中共享用于同一控件的模板。 The implementations of most controls have changed significantly, and they may require different template parts. 大多数控件的实现已发生重大变化,并且它们可能需要不同的模板部分。 A template which works for a control in Windows 8.1 might not work when compiled for UWP. 为UWP编译时,适用于Windows 8.1中控件的模板可能无法正常工作。

The Visual States for the ComboBoxItem control are different between Windows 8.1 and UWP. Windows 8.1和UWP之间的ComboBoxItem控件的可视状态有所不同。 In Windows 8.1, it has a "SelectionStates" VisualStateGroup containing the "Selected" VisualState, whereas in UWP the "Selected" VisualState is part of the "CommonStates" VisualStateGroup. 在Windows 8.1中,它具有一个“ SelectionStates” VisualStateGroup,其中包含“ Selected” VisualState,而在UWP中,“ Selected” VisualState是“ CommonStates” VisualStateGroup的一部分。

Your template conforms to the Windows 8.1 implementation, not the UWP implementation. 您的模板符合Windows 8.1的实现,而不符合UWP的实现。 You will need to have two separate templates, one for Windows 8.1 and one for UWP. 您将需要有两个单独的模板,一个用于Windows 8.1,一个用于UWP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM