2012-05-31 63 views
1

我想根據選擇狀態切換列表框項目的背景。更改列表框項目的背景圖像

我最初硬編碼的背景在我的ItemTemplate像這樣:

<StackPanel.Background> 
     <ImageBrush ImageSource="PodImages\podstate-Clip.png" /> 
    </StackPanel.Background> 

我試圖進入混合設置基於可視狀態的背景(VS選擇不選擇),但不能弄清楚混合,我仍然不理解造型標記。有人可以幫助我與風格標記或引導我在Blend上我可以設置的位置?

編輯: 好吧,我發現我如何添加列表項的列表框,並設置基於在共混物中的視覺狀態的背景,但我無法弄清楚如何爲DataTemplate中做到這一點項目

編輯:
好吧,我想我要的風格也做會是這樣的(但這個崩潰我的應用程序)

我修改

<Storyboard> 
    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="ImageSource" Storyboard.TargetName="ContentBackground"> 
      <DiscreteObjectKeyFrame KeyTime="0" Value="PodImages/podstate-Clipped.png"/> 
    </ObjectAnimationUsingKeyFrames> 
</Storyboard> 

這裏是風格總數:

<Style x:Key="ListBoxItemStyle2" 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"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="ImageSource" Storyboard.TargetName="ContentBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="PodImages/podstate-Clip.png"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
            <VisualState x:Name="Selected"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="ImageSource" Storyboard.TargetName="ContentBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="PodImages/podstate-Clipped.png"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </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}"> 
          <ContentControl.Background> 
           <ImageBrush Stretch="Fill" x:Name="ContentBackground" ImageSource="PodImages/podstate-Clip.png"/> 
          </ContentControl.Background> 
         </ContentControl> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
+0

現在用一個按鈕。希望有人知道這樣做的好方法。 – earthling

回答