2013-03-03 110 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

相关问题