【问题标题】:Why is there nothing defined in ItemContainerStyle states in WPF listbox?为什么 WPF 列表框中的 ItemContainerStyle 状态中没有定义任何内容?
【发布时间】:2011-07-15 09:56:55
【问题描述】:

当我编辑模板并进入 ItemContainerStyle 模板时,状态被定义为 Unselected、Selected、MouseOver 等。但实际上没有对这些状态进行任何操作。这种行为正常吗?我使用过 Silverlight 列表框。在其中,ItemContainerStyle 确实定义了所有状态,并且许多属性都已更改。 WPF 列表框有点令人困惑。看到这是我编辑 ItemContainerStyle 时的 xaml:

<Style x:Key="ListBoxItemContainerStyle" TargetType="{x:Type ListBoxItem}">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="Padding" Value="2,0,0,0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true" >
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver"/>
                                    <VisualState x:Name="Disabled"/>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="SelectionStates">
                                    <VisualState x:Name="Unselected"/>
                                    <VisualState x:Name="Selected"/>
                                    <VisualState x:Name="SelectedUnfocused"/>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

这里只给出了一个 ContentPresenter。我想在鼠标悬停后更改渐变。我还想缩放项目的大小。我该怎么做?

提前致谢:)

【问题讨论】:

  • 在 MouseOver 上,您只是想“快速”更改背景还是希望更改动画?

标签: .net wpf xaml styles wpf-4.0


【解决方案1】:

在 WPF 中使用状态时,您必须在所需状态内定义一个 Storyboard 并以动画的形式进行更改。 如果您想更改项目容器的某些属性,例如,在 Borders 样式内的 IsMouseOver 属性上使用 Trigger 可能会更简单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多