WP8_访问ListBox中的Item项中的某个元素

How to access a Control placed inside ListBox ItemTemplate in WP7(转)

In this post I am going to talk about how to access a Control inside the ListBox ItemPanelTemplate/DataTemplate in Silverlight for WP7.

Question: How to access/modify a specific Control placed inside ListBox ItemTemplate/DataTemplate?

When you have a data bound control, lets say for example ListBox we usually add some custom DataTemplate. So sometimes you try to access and modify any element inside DataTemplate. 

Answer: Actually you can this by using the VisualTreeHelper which provides utility methods that can used to traverse object relationships (along child object or parent object axes) in the Silverlight for WP7 visual tree.

To begin with lets create a sample Windows Phone 7 project , add a data bound to a collection of strings ListBox  with the following ItemsTemplate and ItemsPanel:

 
<ListBox x:Name="list"> 
 
<ListBox.ItemTemplate> 
 
<DataTemplate> 
 
<StackPanel> 
 
<ToggleButton x:Name="btnToggle"/> 
 
<CheckBox x:Name="cbx"/> 
 
<TextBlock Text="{Binding}"/> 
 
</StackPanel> 
 
</DataTemplate> 
 
</ListBox.ItemTemplate> 
 
</ListBox>
 
 
List<string> dataSource = new List<string> {"Item1","Item2","Item3" }; 
 
this.list.ItemsSource = dataSource;

How to Access a specific Control placed inside  ListBox ItemsTemplate

Here is how you can implement a Generic method that can find the first element from any particular type inside the Visual Tree:

Example1:  Use a Generic method to find first element of particular type:

 
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; 
 
}
 

Sample usage:

We will first get an instance of the second Listbox item using ItemContainerGenerator. (Note that we have to use ItemContainerGenerator because our ListBox is databound!). Next we will find the CheckBox control which is inside the ListBox itema and will set its IsChecked property to true:

 
ListBoxItem item = this.list.ItemContainerGenerator.ContainerFromIndex(2) as ListBoxItem; 
 
CheckBox tagregCheckBox = FindFirstElementInVisualTree<CheckBox>(item); 
 
tagregCheckBox.IsChecked = true;

Example2:  Use a simple method to find first element of a particular type that meets a condition:
 
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 TextBlock) 
 
{ 
 
TextBlock targetItem = (TextBlock)child; 
 
 
if (targetItem.Text == "Item2") 
 
{ 
 
targetItem.Foreground = new SolidColorBrush(Colors.Green); 
 
return; 
 
} 
 
} 
 
else
 
{ 
 
SearchVisualTree(child); 
 
} 
 
} 
 
}
 

Sample usage:

This code will find the first TextBlock element in the ListBox which has Text set to "Item2"

 
SearchVisualTree(this.list);

NOTE: In this way you can implement your own methods that find all element in the VisualTree of  a type,  you can add another condition, etc.

I hope that the post was helpful. The full source code is available here.

原文链接:http://www.windowsphonegeek.com/tips/how-to-access-a-control-placed-inside-listbox-itemtemplate-in-wp7

 

 

 

另外:修改listbox的datatemplate中的控件属性方法:

 
public static void SearchVisualTree(DependencyObject targetElement, int width,string Node)
        {
            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 TextBlock)
                {
                    TextBlock targetItem = (TextBlock)child;
                    if (targetItem.Name == Node)
                        targetItem.Width = width;
                }
                else
                {
                    SearchVisualTree(child, width,Node);
                }
            }
        }

调用:SearchVisualTree(listbox1,500,"textBox2");

posted on 2014-09-04 00:38  v.e.n.u.s 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/jx270/p/3955128.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值