2013-10-11 197 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> 
相關問題