2013-10-11 58 views
2

首先我在这里和网上搜索,我发现很多很多解决方案如何在WPF中的列表框中更改所选项目的背景颜色,但不知道如何在Windows应用商店中做到这一点。这个框架是litte不同我不能工作的任何解决方案。更改列表框中选定项目的背景颜色

我用这个:http://social.msdn.microsoft.com/Forums/windowsapps/en-US/91575930-2058-413a-99de-f3b31c74dfd9/change-itemtemplate-forground-when-listbox-is-focused?forum=winappswithcsharp在页面的末端是很好的解决方案,但他设定项teplate这样的: ItemTemplate="{StaticResource DataTemplate1}" 但我的列表已datateplate所以我不知道通过制定者或任何不同的方式如何设定的ItemTemplate风格。

我的列表框:

<ListBox x:Name="lbMenu" ItemsSource="{Binding MyDataForLunchGrid}" Tapped="lbMenzaMenu_Tapped" Style="{StaticResource ListBoxStyle1}"> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
      <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
      <Setter Property="Style" Value="{StaticResource ListBoxItemStyle1}"/> 
     </Style> 
    </ListBox.ItemContainerStyle > 
    <ListBox.ItemTemplate > 
     <DataTemplate> 
      <Grid> 
       <TextBlock Foreground="#FF19536E" x:Name="tbMenu" Text="{Binding launchItemName}"/> 
       <TextBlock x:Name="tbMenuNumber" Text="{Binding launchNumber}"/> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

现在,当我按下列表框中的任何项目的有深紫色(默认)颜色和它的外观horible。

回答

3

如何更改选定项的背景颜色列表框中

我想你想改变你的ItemContainerStyle的定义。尝试是这样的:

<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle1}" ... 

资源“ListBoxItemStyle1”应包含控制模板ListBoxItem

<Style TargetType="ListBoxItem" x:Name="ListBoxItemStyle1"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListBoxItem"> 
       <!-- template here --> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

反过来控制模板定义“选定”视觉状态。从page you linked“ListBoxItemStyle1”定义了视觉状态如下(黄色背景):

<VisualState x:Name="Selected"> 
    <Storyboard> 
     <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid"> 
      <DiscreteObjectKeyFrame KeyTime="0" Value="Yellow"/> 
     </ObjectAnimationUsingKeyFrames> 
     <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
      <DiscreteObjectKeyFrame KeyTime="0" Value="Green"/> 
     </ObjectAnimationUsingKeyFrames> 
    </Storyboard> 
</VisualState> 

注意,默认情况下,ListBoxItem中的“选中”状态作为它的背景使用用户的当前“口音刷”,如见下面。这可能是你看到的深紫色的来源。 (你可以找到在Windows Phone SDK folder所有默认样式和模板。)

<VisualState x:Name="Selected"> 
    <Storyboard> 
     <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentContainer" Storyboard.TargetProperty="Foreground"> 
      <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/> 
     </ObjectAnimationUsingKeyFrames> 
    </Storyboard> 
</VisualState> 

您可以修改此需要 - 复制 - 粘贴默认样式,无论是从Windows SDK,或者从链接的页面,并设置背景和其他属性,无论你想要什么。

有关控制模板和视觉状态的更多背景信息,请参阅Customizing the Appearance of an Existing Control by Using a ControlTemplate

1

我只是有同样的问题。选择项目时,我想摆脱默认的蓝紫色。即使有这篇文章作为帮助,我花了一段时间才弄清楚我必须改变ItemContainerStyle中的哪个VisualState。所以我想我只是在这里发布。这就是我所做的:

<VisualStateManager.VisualStateGroups>   
    <VisualStateGroup x:Name="SelectionStates">   
     <VisualState x:Name="SelectedPointerOver"> 
      <Storyboard> 
       <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid"> 
        <DiscreteObjectKeyFrame KeyTime="0" Value="[NOT-PURPLE-BLUE-ANYMORE]"> 
       </ObjectAnimationUsingKeyFrames> 
       <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListBoxItemSelectedForegroundThemeBrush}"/> 
       </ObjectAnimationUsingKeyFrames> 
      </Storyboard> 
     </VisualState> 
    </VisualStateGroup> 
</VisualStateManager.VisualStateGroups>