2014-02-24 63 views

回答

0

從試試這個

private void listLocs_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    ListBoxItem myitem = listLocs.SelectedItem as ListBoxItem; 
    SolidColorBrush brush = new SolidColorBrush(Color.FromArgb(255,255,0,0)); 
    myitem.Background = brush; 
} 

更多細節: How can I change selected item's background color in Windows Phone?

+0

是否恢復項目的一個視圖,當我選擇另一個項目? – splash27

+0

這應該做到這一點,但萬一不是你可以讓循環之前的代碼重置爲默認顏色... – Adel

+0

似乎不起作用。它拋出NullReferenceException。也許很重要的一點是,我使用ListBox的自定義ItemTemplate,其中只包含StackPanel中的TextBlock。 – splash27

0

您可以通過ListBox的ItemContainerStyle屬性造型的ListBoxItem中做到這一點。使用以下步驟

  1. 在Visual Studio(或Blend)中打開包含您的ListBox的頁面(xaml)。
  2. 查看設計。
  3. 右鍵單擊列表框,然後選擇編輯其他模板 - >編輯生成的項容器(ItemContainerStyle)
  4. 這種風格
  5. 在選擇狀態選擇一個名稱的位置添加一個新的動畫背景。

所以,你的故事板將改變從:

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

要:

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

不幸的是,它不起作用。我應該創建新的空的還是編輯現有的ItemContainer樣式? – splash27

相關問題