UWP ComboBox中是否有“Unselected”VisualState的等价物?我们正在从Windows 8迁移,并且想要一个彼此交叉兼容的解决方案。
我查看了ComboBoxItem的默认样式,而UWP似乎没有“取消选择”的VisualState。
以下是发生的事情的屏幕截图:
蓝色一个是已选择的项目。由于UWP中没有“未选中”视觉状态,因此不会取消选择之前选择的项目。
这是我的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>
谢谢!
答案 0 :(得分:1)
我不建议在Windows 8 / 8.1和UWP上共享同一控件的模板。大多数控件的实现已发生显着变化,并且可能需要不同的模板部件。在为UWP编译时,适用于Windows 8.1中的控件的模板可能不起作用。
ComboBoxItem控件的可视状态在Windows 8.1和UWP之间是不同的。在Windows 8.1中,它有一个&#34; SelectionStates&#34; VisualStateGroup包含&#34; Selected&#34; VisualState,而在UWP中,&#34;选择&#34; VisualState是&#34; CommonStates&#34;的一部分。 VisualStateGroup。
您的模板符合Windows 8.1实施,而不符合UWP实施。您需要有两个单独的模板,一个用于Windows 8.1,另一个用于UWP。