How to change the list template of selected WP7 items in a dynamic data template?

I have a list in an application that uses a dynamic element template defined in app.xaml and attached to it during C # code execution.

The fact is that I want to change the background of the selected element, but there is no template on my .xaml page for editing a copy of the element template for the selected state.

Is there a way to change the selected item in C #. Or even determine the state in app.xaml to pass it to the list?

0
source share
1 answer

. 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>
0

All Articles