Как изменить фон списка выбранных элементов WP7 в шаблоне динамических данных? - PullRequest
0 голосов
/ 14 февраля 2012

В моем приложении есть список, который использует динамический шаблон элемента, определенный в app.xaml и присоединенный к нему во время выполнения в коде C #.

Дело в том, что я хочу изменить фон выбранного элемента, но в моем page.xaml нет шаблона для редактирования копии шаблона элемента для выбранного состояния.

Есть ли способ изменить фон выбранного элемента из C #. Или даже определить состояние в app.xaml, чтобы передать его в список?

1 Ответ

0 голосов
/ 14 февраля 2012

Есть.Если у вас есть доступ к Expression Blend, вы сможете изменить XAML по умолчанию, чтобы изменить способ обработки выбранного состояния в VisualStateManager.Также можно найти используемый по умолчанию XAML и переопределить его в App.Xaml.

По сути, вам нужно определить новый ресурс стиля для ListBoxItem и применить его к списку.Например ...

<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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...