如何更改动态数据模板中的 WP7 列表 SelectedItem 背景? [英] How to change the WP7 List SelectedItem Background in Dynamic Data template?

查看:19
本文介绍了如何更改动态数据模板中的 WP7 列表 SelectedItem 背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个列表,它使用在 app.xaml 中定义的动态项目模板,并在运行时以 C# 代码附加到它.

I have a list in my application which uses a dynamic item template defined in the app.xaml and attached to it in the run time in C# code.

问题是我想更改所选项目的背景,但我的 page.xaml 中没有可用模板来编辑所选状态的项目模板副本.

The thing is I want to change the background of the selected item, but there is no template available in my page.xaml to edit a copy of the item template for the selected state.

有没有办法从 C# 更改所选项目的背景.或者甚至在 app.xaml 中定义一个状态以将其添加到列表中?

Is there is a way to change the selected item background from the C#. Or even define a state in app.xaml to give it to the list?

推荐答案

有.如果您有权访问 Expression Blend,您将能够修改默认 XAML 以更改 VisualStateManager 中选定状态的处理方式.还可以找到使用的默认 XAML 并在 App.Xaml 中覆盖它.

There is. If you have access to Expression Blend, you will be able to modify the default XAML to change how the selected state in VisualStateManager is handled. It is also possible to find the default XAML that is used and override that in App.Xaml.

本质上,您需要为 ListBoxItem 定义一个新的样式资源,并将其应用于列表框.例如...

Essentially, you need to define a new style resource for the ListBoxItem, and apply that to the listbox. For example...

<ListBox Margin="0" Name="MyListBox" ItemContainerStyle="{StaticResource ListBoxItemStyle1}" />


<Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem">
  <Setter Property="Background" Value="Transparent"/>
  <Setter Property="BorderThickness" Value="0"/>
  <Setter Property="BorderBrush" Value="Transparent"/>
  <Setter Property="Padding" Value="0"/>
  <Setter Property="HorizontalContentAlignment" Value="Left"/>
  <Setter Property="VerticalContentAlignment" Value="Top"/>
  <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"/>
            <VisualState x:Name="Disabled">
              <Storyboard>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot">
                  <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TransparentBrush}"/>
                </ObjectAnimationUsingKeyFrames>
                <DoubleAnimation Duration="0" To=".5" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ContentContainer"/>
              </Storyboard>
            </VisualState>
          </VisualStateGroup>
          <VisualStateGroup x:Name="SelectionStates">
            <VisualState x:Name="Unselected"/>
<!-- This is the bit you are specifically interested in -->
              <VisualState x:Name="Selected">
                <Storyboard>
                   <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                     <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush"/>
                   </ObjectAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
<!-- This is the end of the bit you are specifically interested in -->
            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
          <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
        </Border>
      </ControlTemplate>
    </Setter.Value>
    </Setter>
  </Style>

这篇关于如何更改动态数据模板中的 WP7 列表 SelectedItem 背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆