您的位置:首页 > 其它

VisualTreeHelper遍历listbox遇到的问题,测试解决办法

2014-02-26 15:47 369 查看
VisualTreeHelper遍历listbox找到要找的stackpanel后会在同一个stackpanel中遍历2遍不知道什么原因,暂时只能让这2遍全部执行,导出,全选反选能做到

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Threading;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Navigation;

using Microsoft.Phone.Controls;

using Microsoft.Phone.Shell;

using C1.Phone;

using System.Windows.Media;

namespace jbgswin

{

public partial class peoplelist : PhoneApplicationPage

{

public peoplelist()

{

InitializeComponent();

this.lst.ItemsSource = App.mygc.ToList();

}

private void Button_Click(object sender, RoutedEventArgs e)

{

for (int i = 0; i < this.lst.Items.Count; i++)

{

C1ListBoxItem lb = this.lst.ItemContainerGenerator.ContainerFromIndex(i) as C1ListBoxItem;

// DataTemplate da = lb.FindName("DataTemplate1") as DataTemplate;

CheckBox cka = FindFirstElementInVisualTree<CheckBox>(lb) as CheckBox;

// StackPanel stack = (StackPanel)lb.FindName("sa");

// CheckBox cka = stack.FindName("ck") as CheckBox;

cka.IsChecked = true;

}

}

private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject

{

var count = VisualTreeHelper.GetChildrenCount(parentElement);

var b = count;

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 Button_Click_1(object sender, RoutedEventArgs e)

{

for (int i = 0; i < this.lst.Items.Count; i++)

{

C1ListBoxItem lb = this.lst.ItemContainerGenerator.ContainerFromIndex(i) as C1ListBoxItem;

// DataTemplate da = lb.FindName("DataTemplate1") as DataTemplate;

CheckBox cka = FindFirstElementInVisualTree<CheckBox>(lb) as CheckBox;

// StackPanel stack = (StackPanel)lb.FindName("sa");

// CheckBox cka = stack.FindName("ck") as CheckBox;

if (cka.IsChecked == true)

{

App.toimport.Add(App.mygc.ToList().Where(h=>h.Id.ToString()==cka.Tag.ToString()).FirstOrDefault());

}

}

NavigationService.Navigate(new Uri("/chakan.xaml", UriKind.Relative));

// MessageBox.Show(App.toimport.Count.ToString());

}

private void Button_Click_2(object sender, RoutedEventArgs e)

{

for (int i = 0; i < this.lst.Items.Count; i++)

{

C1ListBoxItem lb = this.lst.ItemContainerGenerator.ContainerFromIndex(i) as C1ListBoxItem;

// DataTemplate da = lb.FindName("DataTemplate1") as DataTemplate;

/* CheckBox cka = FindFirstElementInVisualTree<CheckBox>(lb) as CheckBox;

int a = 0;

// StackPanel stack = (StackPanel)lb.FindName("sa");

// CheckBox cka = stack.FindName("ck") as CheckBox;

if (cka.IsChecked == true)

{

// App.toimport.Add(App.mygc.ToList().Where(h => h.Id.ToString() == cka.Tag.ToString()).FirstOrDefault());

NavigationService.Navigate(new Uri("/addpeople.xaml?id=" + cka.Tag.ToString(), UriKind.Relative));

}*/

searchtonav(lb);

}

}

private void Button_Click_3(object sender, RoutedEventArgs e)

{

for (int i = 0; i < this.lst.Items.Count; i++)

{

C1ListBoxItem lb = this.lst.ItemContainerGenerator.ContainerFromIndex(i) as C1ListBoxItem;

/*StackPanel sp = FindFirstElementInVisualTree<StackPanel>(lb) as StackPanel;

// DataTemplate da = lb.FindName("DataTemplate1") as DataTemplate;

CheckBox cka = sp.FindName("ck") as CheckBox;

int c = 0;

// StackPanel stack = (StackPanel)lb.FindName("sa");

// CheckBox cka = stack.FindName("ck") as CheckBox;

cka.IsChecked = false;*/

SearchVisualTree(lb);

}

}

private void searchtonav(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;

CheckBox cka = targetItem.FindName("ck") as CheckBox;

int c = 0;

// StackPanel stack = (StackPanel)lb.FindName("sa");

// CheckBox cka = stack.FindName("ck") as CheckBox;

if (cka.IsChecked==true)

{

NavigationService.Navigate(new Uri("/addpeople.xaml?id=" + cka.Tag.ToString(), UriKind.Relative));

}

return;

}

else

{

searchtonav(child);

}

}

}

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;

CheckBox cka = targetItem.FindName("ck") as CheckBox;

int c = 0;

// StackPanel stack = (StackPanel)lb.FindName("sa");

// CheckBox cka = stack.FindName("ck") as CheckBox;

cka.IsChecked = false;

return;

}

else

{

SearchVisualTree(child);

}

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: