【问题标题】:Change Border Background property on Selection更改选择的边框背景属性
【发布时间】:2014-02-02 22:04:16
【问题描述】:

当列表框的ListBoxItem 被选中时,我想更改Border.Background

我在 App.xaml 中制作了这个资源:

<Style x:Key="HighlightStyle" TargetType="ListBoxItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver"/>

                        </VisualStateGroup>
                        <VisualStateGroup x:Name="SelectionStates">
                            <VisualState x:Name="Unselected"/>
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <SolidColorBrush Color="#FFE20080" />
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            <VisualState x:Name="SelectedUnfocused"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

奇怪,它适用于Foreground,它改变了颜色,但它不适用于背景,这就是我想要改变的。 XAML 中没有设置 Background 属性,因此那里没有本地默认值。

【问题讨论】:

    标签: c# xaml windows-phone-7 background listbox


    【解决方案1】:

    尝试在 setter 中以您的风格设置背景。动画未运行的原因可能是主题中的画笔被冻结。见http://msdn.microsoft.com/en-us/library/ms750509(v=vs.110).aspx

    --- 编辑 --- 抱歉,昨晚我回答的时候已经很晚了,我错过了几件事:) 其中最重要的是这个问题与 Windows Phone 有关。

    首先,您不需要为颜色设置动画,而是要替换画笔 - 这样会更有效率。

    其次,您可能想要更改列表框项的背景,而不是更改它的特定组件。

    以下几行实现了这一点:

    <VisualState x:Name="Selected">
        <Storyboard>
          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background">
            <DiscreteObjectKeyFrame KeyTime="0">
              <DiscreteObjectKeyFrame.Value>
                <SolidColorBrush Color="#FFE20080" />
              </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
          </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </VisualState>
    

    然后,您的容器控件组件上有一个硬编码值(“White”)。让它继承列表框项的值,如下所示:

    <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"/>
    

    HTH

    【讨论】:

    • 谢谢,我已经尝试并编辑了上面的代码,请看一下。
    • 我已经试过你的代码,添加了一个属性: Storyboard.TargetName="ContentContainer" 然后它不会做任何事情。使用前景再次很好,但没有背景。
    • 我没有 WP7 可以尝试这个...但是您必须添加 TargetName 吗?没有它就不行吗?在其他 XAML 环境中,该语法将针对模板化元素的背景属性。我不知道 WP7 是否不支持此功能。
    • 它在没有 TargetName 的情况下崩溃了,所以我添加了它。奇怪的是它可以与 Foreground 一起使用,所以语法应该很好。
    • 好吧...在这种情况下,尝试使用“LayoutRoot”作为 TargetName,并将“ContentContainer”的背景设置为“{x:Null}”或“透明”。这可能是运行时中的一个奇怪的小错误。
    猜你喜欢
    • 1970-01-01
    • 2019-06-18
    • 2011-03-29
    • 2012-10-01
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    相关资源
    最近更新 更多