2012-02-14 4 views
0

app.xamlで定義された動的項目テンプレートを使用し、実行時にC#コードで添付されたアプリケーションのリストがあります。WP7リストSelectedItemを変更する方法ダイナミックデータテンプレートの背景?

選択したアイテムの背景を変更したいのですが、選択した状態のアイテムテンプレートのコピーを編集するために、自分のpage.xamlにテンプレートがありません。

選択した項目の背景をC#から変更する方法はありますか?あるいは、それをリストに与えるためにapp.xamlの状態を定義することさえできますか?

答えて

0

あります。 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

問題は私の主なターゲットは、これはの単なる一例であるバックグラウンド – Nariman

+0

を変化させている、彼は背景を変更しないバックグラウンド画像、さらにはストーリーボードを変更したことがないということですあなたのニーズに合うように*入力して編集する必要があるコード* – ZombieSheep

+0

背景を変更しようとしましたが、それは私にとってはうまくいきませんでした。私が間違っているかどうか分かりません。右のサンプルコードを投稿してください 注:選択すると不透明度が変更されましたが、背景としては機能しませんでした – Nariman

関連する問題