博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WP7中对ListBox的ItemTemplate中子元素的后台操作
阅读量:4652 次
发布时间:2019-06-09

本文共 3045 字,大约阅读时间需要 10 分钟。

为了使自己开发的软件更加适应Windows Phone 7所提供的两套黑白主题,我们需要对主题进行判断,然后做出不同的控件外观显示效果。比如要完成一个好友列表显示,在列表的每个listbox item中的背景需要根据用户当前所选择的主题来分别显示不同的颜色,先看看前台的代码:
那么如何获取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:背景我采用了两张图片。
方法二:
还有个方法来进行操作,可以看下面方法,方法是参照 网站上的,大家也可以看看。
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

你可能感兴趣的文章
html基础知识
查看>>
IO流2之文件夹的
查看>>
好的用户界面-界面设计的一些技巧
查看>>
【Android实战开发】3G技术和Android发展简介
查看>>
【精解】EOS标准货币体系与源码实现分析
查看>>
AFore.NET 翻译
查看>>
[大牛翻译系列]Hadoop(8)MapReduce 性能调优:性能测量(Measuring)
查看>>
What to do when the Chinese Characters are messed up when extracting from zip archive?
查看>>
SQLYog快捷键大全
查看>>
(转载)DLL动态链接库编程入门之三:MFC规则DLL(上)
查看>>
隐藏Nginx或Apache以及PHP的版本号的方法
查看>>
N32926之jlink调试配置
查看>>
ASP.NET ACCESS 分页
查看>>
HashMap
查看>>
Android广播机制概述
查看>>
mysql触发器
查看>>
我是怎么让全国最大的儿童失踪预警平台流量掉底的
查看>>
领扣(LeetCode)二叉树的中序遍历 个人题解
查看>>
MySQL5.5登录密码忘记了,怎嘛办?
查看>>
[javascript]9宫格拖拽拼图游戏 puzzle
查看>>