WP7中對ListBox的ItemTemplate中子元素的後臺操作

爲了使自己開發的軟件更加適應Windows Phone 7所提供的兩套黑白主題,我們需要對主題進行判斷,然後做出不同的控件外觀顯示效果。比如要完成一個好友列表顯示,在列表的每個listbox item中的背景需要根據用戶當前所選擇的主題來分別顯示不同的顏色,先看看前臺的代碼:
複製代碼
<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>
複製代碼
那麼如何獲取ListBox中的StackPanel這個子元素的值呢,又如何來對每個Item的背景色進行改變呢?我們可以用下面的方法來進行判斷,首先獲取當前系統所使用的背景色,然後遍歷ListBox中的每個item,利用泛型函數對該ListBox的整個visual tree進行讀取,根據需要選擇StackPanel類型的控件(這裏讀者可以根據實際情況做改動。)
方法一:
複製代碼
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;
}
複製代碼
好了看看效果吧(只是個示例,更炫的效果還需要讀者自己斟酌了呵呵)PS:背景我採用了兩張圖片。
方法二:
還有個方法來進行操作,可以看下面方法,方法是參照WindowsPhoneGeek網站上的,大家也可以看看。
複製代碼
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);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章