2013-03-03 238 views
0

如何更改在Windows Phone中包含數據模板的列表框中的選定項目的背景顏色?如何更改Windows Phone中選定項目的背景顏色?

我已經看到它可以與Setter屬性。我會在哪寫他們?

謝謝。

代碼

<ListBox x:Name="listLocs" HorizontalAlignment="Left" Height="605" VerticalAlignment="Top" Width="250" SelectionChanged="listLocs_SelectionChanged" Margin="10,155,0,0" BorderBrush="#FF030042" BorderThickness="2" Foreground="#FF030042"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <StackPanel> 
        <Image Source="/Images/Pin2.png" Width="60" Height="60" /> 
       </StackPanel> 
       <StackPanel> 
        <StackPanel> 
         <TextBlock x:Name="txtName" Margin="10,0,0,0" Foreground="#FF030042" FontSize="30" Text="{Binding Name}"/> 
        </StackPanel> 
        <StackPanel> 
         <TextBlock x:Name="txtDescription" Margin="10,0,0,0" Foreground="#FF030042" FontSize="20" Text="{Binding Description}"/> 
        </StackPanel> 
       </StackPanel> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

您可以檢查這個問題:選中時 ListBoxItem的背景 [更改顏色和不重點] [http://stackoverflow.com/questions/7298282/listbox-selected-item-background] [1]:http:///stackoverflow.com/questions/7298282/listbox-selected-item-background – 2013-03-03 09:44:04

+0

謝謝,但我不明白,因爲它的額外:( – 2013-03-03 20:44:48

回答

1

你可以做到這一點在後面selectchanged事件處理altough代碼:

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; 
    } 
+0

謝謝,但它不工作。 myitem採取空值。 – 2013-03-04 15:15:17

+0

這意味着是不是項目被選中是否確定您選擇了一個項目?您可以檢查調試模式中返回的類型。 – 2013-03-05 07:51:16

+0

是的,我確定,我從我的數據模板課獲取名稱並將其顯示在標籤中。 – 2013-03-07 13:43:48

相關問題