在选择上更改边框背景属性

时间:2014-02-02 22:04:17

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

我想在选择ListBox的Border.Background时更改ListBoxItem

我在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,它会改变颜色,但它不适用于Background,这就是我想要改变的。 Background属性未在XAML中设置,因此没有本地默认值。

1 个答案:

答案 0 :(得分:1)

尝试在设置器中设置样式中的背景。动画未运行的原因可能是主题中的画笔已冻结。见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