<ListBox x:Name="FirstListBox" ItemsSource="{Binding mFriends}" Margin="0,-6,-12,0" Height="541">
<ListBox.ItemTemplate>
<DataTemplate>
<Border x:Name="borderListBox" Margin="5" CornerRadius="3">
<StackPanel Orientation="Horizontal">
<Image Height="80" Margin="20" Width="80" Source="{Binding Image_Url}">
</Image>
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Margin="0,10,0,0" Text="{Binding Name}" FontFamily="/Fonts/YGY20070701.ttf#葉根友鋼筆行書簡體" Foreground="{StaticResource UseNameUnSelectedBrush}" TextWrapping="Wrap" FontSize="28"/>
<Image Name="VipImage" Source="{Binding Vip}" Width="32" Height="32"></Image>
</StackPanel>
<TextBlock Width="328" Text="{Binding message}" Foreground="Black" TextWrapping="Wrap" />
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
var back = Application.Current.Resources["PhoneBackgroundColor"].ToString();
if (back == "#FF000000")
{
for (int i = 0; i < FirstListBox.Items.Count; i++)
{
ListBoxItem item = this.FirstListBox.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
StackPanel border = FindFirstElementInVisualTree<StackPanel>(item);
border.Background = new SolidColorBrush(Color.FromArgb(170, 255, 255, 255));
}
}
else
{
for (int i = 0; i < FirstListBox.Items.Count; i++)
{
ListBoxItem item = this.FirstListBox.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
StackPanel border = FindFirstElementInVisualTree<StackPanel>(item);
border.Background = new SolidColorBrush(Color.FromArgb(255, 34, 34, 34));
}
}
private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject{var count = VisualTreeHelper.GetChildrenCount(parentElement);if (count == 0)return null;for (int i = 0; i < count; i++){var child = VisualTreeHelper.GetChild(parentElement, i);if (child != null && child is T){return (T)child;}else{var result = FindFirstElementInVisualTree<T>(child);if (result != null)return result;}}return null;}
private void SearchVisualTree(DependencyObject targetElement)
{
var count = VisualTreeHelper.GetChildrenCount(targetElement);
if (count == 0)
return; for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(targetElement, i);
if (child is StackPanel)
{
StackPanel targetItem = (StackPanel)child;
var back = Application.Current.Resources["PhoneBackgroundColor"].ToString();
if (back == "#FF000000")
{
targetItem.Background = new SolidColorBrush(Color.FromArgb(255, 34, 34, 34));
}
else
{
targetItem.Background = new SolidColorBrush(Color.FromArgb(170, 255, 255, 255));
}
}
else
{
SearchVisualTree(child);
}
}
}
this .SearchVisualTree( this .FirstListBox); |