我想根据选择状态切换列表框项目的背景。
我最初在我的 ItemTemplate 中硬编码背景,如下所示:
<StackPanel.Background>
<ImageBrush ImageSource="PodImages\podstate-Clip.png" />
</StackPanel.Background>
我尝试进入 Blend 以根据视觉状态(选择与未选择)设置背景,但无法弄清楚 Blend,我仍然不了解样式标记。有人可以帮助我处理样式标记或在 Blend 中指导我在哪里设置它吗?
编辑:好的,我找到了如何将列表项添加到列表框并根据混合中的视觉状态设置背景,但我不知道如何为数据模板项执行此操作
编辑:
好的,我想我想做的事情是这样的(但这会使我的应用程序崩溃)
我修改了
<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>