为了使自己开发的软件更加适应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(inti =0; i <FirstListBox.Items.Count; i++)
{
ListBoxItem item =this.FirstListBox.ItemContainerGenerator.ContainerFromIndex(i) asListBoxItem;
StackPanel border =FindFirstElementInVisualTree<StackPanel>(item);
border.Background =newSolidColorBrush(Color.FromArgb(170, 255, 255, 255));
}
}
else
{
for(inti =0; i <FirstListBox.Items.Count; i++)
{
ListBoxItem item =this.FirstListBox.ItemContainerGenerator.ContainerFromIndex(i) asListBoxItem;
StackPanel border =FindFirstElementInVisualTree<StackPanel>(item);
border.Background =newSolidColorBrush(Color.FromArgb(255, 34, 34, 34));
}

}
privateT FindFirstElementInVisualTree<T>(DependencyObject parentElement) whereT : DependencyObject

{
var count =VisualTreeHelper.GetChildrenCount(parentElement);
if(count ==0)
returnnull;
for(inti =0; i <count; i++)
{
var child =VisualTreeHelper.GetChild(parentElement, i);
if(child !=null&&child isT)
{
return(T)child;
}
else
{
var result =FindFirstElementInVisualTree<T>(child);
if(result !=null)
returnresult;
}
}
returnnull;
}

好了看看效果吧(只是个示例,更炫的效果还需要读者自己斟酌了呵呵)PS:背景我采用了两张图片。
方法二:
还有个方法来进行操作,可以看下面方法,方法是参照WindowsPhoneGeek网站上的,大家也可以看看。
privatevoid 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);

转载于:https://www.cnblogs.com/songtzu/archive/2012/11/02/2750783.html

WP7中对ListBox的ItemTemplate中子元素的后台操作相关推荐

  1. html5盒子层级设置,解决CSS中子元素z-index与父元素兄弟节点的层级问题

    1.问题的出现 写了一个平铺的列表,其中有些列表项具有hover出现的弹出框.希望达成的目标是弹出框展现时,要把列表项内容遮盖住,以保证弹出框内容优先展示. 元素的结构大致如下: 列表项1 弹出框1 ...

  2. python list元素合并_Python程序中使用表达式合并List列表元素 - Python - 服务器之家...

    Python程序中使用表达式合并List列表元素 发布时间:2014-02-06 来源:服务器之家 在实现一个产品过程中,使用 Python Extension 需要一个语句将一个列表中子列表合并成为 ...

  3. CSS样式中伪类和伪类元素的区别(css中一个冒号和两个冒号的区别)

    首先介绍下什么是伪类,所谓伪类就是: 伪类选择元素基于的是当前元素处于的状态,或者说元素当前所具有的特性,而不是元素的id.class.属性等静态的标志.由于 状态是动态变化的,所以一个元素达到一个特 ...

  4. php判断数组不重复的元素,php从数组中随机选择若干不重复元素

    php从数组中随机选择若干唯一元素 /* * $array = the array to be filtered * $total = the maximum number of items to r ...

  5. 【Groovy】集合遍历 ( 使用集合的 findAll 方法查找集合中符合匹配条件的所有元素 | 代码示例 )

    文章目录 一.使用集合的 findAll 方法查找集合中符合匹配条件的所有元素 1.闭包中使用 == 作为 findAll 方法的查找匹配条件 2.闭包中使用 is 作为 findAll 方法的查找匹 ...

  6. leetcode 215.数组中的第K个最大元素

    难度:中等 频率:250 ** 题目:给定整数数组nums和整数k,清返回数组中第K个最大的元素. 清注意你要找的是数组排序后的第K个最大的元素,而不是第K个不同的元素. ** ** 题目类型: 经典 ...

  7. 《漫画算法2》源码整理-8 链表中倒数第K个节点元素

    链表中倒数第K个节点元素 public class KthFromEnd {public static Node findKthFromEnd(Node head, int k){Node p1 = ...

  8. 找出一个数组中出现次数最多的那个元素

    Description 找出一个数组中出现次数最多的那个元素 Input 多组输入,请处理到文件结束 每组第一行输入一个整数n(不大于20) 第二行输入n个整数 Output 找出n个整数中出现次数最 ...

  9. python中列表实现自加减元素_python初学者知识整合

    python 第一章:概述 1. 概述 Python是一门跨平台.开源.免费的解释型高级动态编程语言. ① 编译:笔译,用理解原文本 ② 解释:口译,逐字逐句解释,不产生新文本 Python中的可迭代 ...

最新文章

  1. 41、应用如何签名以及签名的意义(转载)
  2. 一个popup弹窗实现思路--(基于mintui分析)
  3. Ambari安装之部署3个节点的HA分布式集群
  4. Java中的==和equals区别
  5. ASP.NET的五大数据控件分析
  6. Eclipse关闭XML文件验证的方法
  7. Eclipse 隐藏已关闭的项目
  8. 大数据学习——yarn集群启动
  9. react-native开发安卓app相关使用总结
  10. win7计算机屏幕休眠,windows7系统怎么设置屏幕不休眠
  11. openCV——轮廓检测
  12. C++语言分号的使用
  13. 强化学习从K-摇臂老虎机开始
  14. 电脑提示Wtautoreg.exe无法找到入口怎么解决?
  15. 动力节点『lol版』Java学习路线图(四)Javaweb阶段
  16. 什么是“可维护性”?
  17. 安卓中COLOR的值分析
  18. Gradle本地化构建技巧之自定义Gradle配置文件
  19. Django之Cookie和 Session
  20. 欢迎潍坊市委组织部副部长都焕德一行莅临润达软件考察指导

热门文章

  1. Codeforces 55D Beautiful numbers (数位DP)
  2. C# MVC 用户登录状态判断
  3. New-Python-数据类型、字符编码、文件处理
  4. Hadoop基础学习
  5. Java问题汇集(1)
  6. jQuery实现列表数据从右至左滚动(类似弹幕)
  7. javascript arguments 特殊 对象
  8. web前端之html从入门到精通
  9. Window10环境下的Jupyter notebook安装与打开默认路径的修改
  10. 6-3 递增的整数序列链表的插入 (10 分)