【问题标题】:C# VisualStateManager - What is the equivalent in WPF for SelectedPointerOver (in Metro)C# VisualStateManager - WPF 中 SelectedPointerOver 的等价物(在 Metro 中)
【发布时间】:2012-06-28 05:58:56
【问题描述】:

我正在为 ListBox 创建一个自定义控件模板,但我在视觉状态方面遇到了困难。使用 VisualState MouseOver 时,它会影响包括所选项目在内的所有项目,我希望它们具有单独的样式。

在 Metro 中,有 SelectedPointerOver,在 WPF 或其他替代方案中是否有任何等效项?

编辑:

例如,所有项目最初都有黑色前景。

当一个项目被选中时,它的前景变为白色(未选中的项目保持黑色前景)。

现在当我将鼠标悬停在未选中的项目上时,我希望它的前景变成蓝色,当我将鼠标悬停在选定的项目上时,我希望它的前景变成红色。

【问题讨论】:

  • 对于哪个控件您想要 SelectedPointerOver 状态...您的意思是任何属性吗?

标签: c# wpf microsoft-metro visualstatemanager


【解决方案1】:

据我所知,WPF 中没有对应的状态。

WPF ListBoxItem 在SelectionStates 组中具有状态UnselectedSelectedSelectedUnfocused,在CommonStates 组中具有状态NormalMouseOverDisabled。每个组中的状态是相互排斥的,但来自不同组的状态可以同时进行。例如,ListBoxItem 可以同时处于状态 SelectedMouseOver

由控制模板定义考虑这一事实的可视化。例如,它可以在选中项目时使用不同的背景画笔填充项目,并在鼠标悬停在项目上时绘制外边框。重要的是存在同时可见的独立可视化,因为相关状态是独立的并且可以同时进行。当一个选定的列表项看起来不被选中时,用户通常会感到有些困惑,因为他将鼠标移到了该项目上。

EDIT - 下面的 ListBox 示例。使用触发器而不是视觉状态可能会更容易完成。

<ListBox SelectionMode="Extended">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Grid Background="LightGray" Margin="1">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="Disabled"/>
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <ColorAnimation Storyboard.TargetName="UnselectedText"
                                                            Storyboard.TargetProperty="Foreground.Color"
                                                            To="Blue" Duration="0:0:0.1"/>
                                            <ColorAnimation Storyboard.TargetName="SelectedText"
                                                            Storyboard.TargetProperty="Foreground.Color"
                                                            To="Red" Duration="0:0:0.1"/>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="SelectionStates">
                                    <VisualState x:Name="Unselected"/>
                                    <VisualState x:Name="Selected">
                                        <Storyboard>
                                            <DoubleAnimation Storyboard.TargetName="UnselectedText"
                                                             Storyboard.TargetProperty="Opacity"
                                                             To="0" Duration="0:0:0.1"/>
                                            <DoubleAnimation Storyboard.TargetName="SelectedText"
                                                             Storyboard.TargetProperty="Opacity"
                                                             To="1" Duration="0:0:0.1"/>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <TextBlock Name="UnselectedText" Margin="2" Text="{TemplateBinding Content}">
                                <TextBlock.Foreground>
                                    <SolidColorBrush Color="Black"/>
                                </TextBlock.Foreground>
                            </TextBlock>
                            <TextBlock Name="SelectedText" Margin="2" Text="{TemplateBinding Content}" Opacity="0">
                                <TextBlock.Foreground>
                                    <SolidColorBrush Color="White"/>
                                </TextBlock.Foreground>
                            </TextBlock>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.Items>
        <ListBoxItem Content="Item 1"/>
        <ListBoxItem Content="Item 2"/>
        <ListBoxItem Content="Item 3"/>
        <ListBoxItem Content="Item 4"/>
        <ListBoxItem Content="Item 5"/>
        <ListBoxItem Content="Item 6"/>
        <ListBoxItem Content="Item 7"/>
        <ListBoxItem Content="Item 8"/>
        <ListBoxItem Content="Item 9"/>
        <ListBoxItem Content="Item 10"/>
    </ListBox.Items>
</ListBox>

【讨论】:

  • 我实际上更想知道是否可以允许基于 IsSelected 属性的可视化变化
  • 这正是视觉状态 SelectedUnselected 的用途。有关处理这些状态的示例 ControlTemplate,请参阅 ListBox Styles and Templates
  • 让我澄清一下。即使 MouseOver 发生,我也希望选中的项目和未选中的项目以不同的方式改变其样式。例如,所有项目最初都有黑色前景。选择一项时,其前景变为白色。现在,当我将鼠标悬停在未选中的项目上时,我希望它的前景变成蓝色,当我将鼠标悬停在选定的项目上时,我希望它的前景变成红色。我希望现在更清楚了。
  • 查看编辑以快速了解您想要做什么。然而它有点难看,因为它有两个相互重叠的 TextBlock,显示相同的文本,只是颜色不同。
猜你喜欢
  • 1970-01-01
  • 2011-04-02
  • 2021-10-15
  • 2013-10-20
  • 2011-04-10
  • 2021-12-02
  • 2010-09-14
  • 2016-12-24
相关资源
最近更新 更多