遇到的问题是在Windows Phone编程开发中,对于ListBox控件,,如何对List中的而已控件进行操作,如取得某个Text的信息。。查找了好半天终于找到解决办法,,现提供如下。
Xaml如下:
代码 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><UserControl x:Class="ToolsTest.Test" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300"> <UserControl.Resources> <DataTemplate x:Key="dt"> <TextBlock Padding="5,0,5,0" Text="{Binding d}" x:Name="myTxt"/> </DataTemplate> </UserControl.Resources> <StackPanel> <ListBox Name="myListBox" ItemTemplate="{StaticResource dt}" /> <Button Content="查找myTxt" x:Name="btnFind" Width="90" Click="btnFind_Click"></Button> </StackPanel> </UserControl>
代码 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System.Collections.ObjectModel; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace ToolsTest { public partial class Test : UserControl { ObservableCollection<TestData> oc; public Test() { InitializeComponent(); this.Loaded += new RoutedEventHandler(Test_Loaded); } void Test_Loaded(object sender, RoutedEventArgs e) { oc = new ObservableCollection<TestData>(); oc.Add(new TestData() { d = "A" }); oc.Add(new TestData() { d = "B" }); this.myListBox.ItemsSource = oc; } private void btnFind_Click(object sender, RoutedEventArgs e) { if (myListBox.SelectedItem != null) { ListBoxItem _selectedItem = (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.SelectedItem)); TextBlock myTxt = FindFirstVisualChild<TextBlock>(_selectedItem, "myTxt"); MessageBox.Show(string.Format("选中行的TextBlock值为:" + myTxt.Text)); } ListBoxItem _firstItem = (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items[0])); //var t = _firstItem.FindName("myTxt");//这样是找不到的 TextBlock myTxtFirst = FindFirstVisualChild<TextBlock>(_firstItem, "myTxt"); MessageBox.Show(string.Format("第一行的TextBlock值为:" + myTxtFirst.Text)); } public T FindFirstVisualChild<T>(DependencyObject obj,string childName) where T : DependencyObject { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { DependencyObject child = VisualTreeHelper.GetChild(obj, i); if (child != null && child is T && child.GetValue(NameProperty).ToString()==childName) { return (T)child; } else { T childOfChild = FindFirstVisualChild<T>(child,childName); if (childOfChild != null) { return childOfChild; } } } return null; } } public class TestData{public string d{set;get;}} }
代码 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><ListBox> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal" x:Name="sp"></StackPanel> </ItemsPanelTemplate> <ListBox.ItemTemplate> <DataTemplate> <Rectangle Width="100" Height="100" Fill="{Binding Color}" x:Name="listItem" MouseLeftButtonDown="listItem_MouseLeftButtonDown"></Rectangle> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
如果想在listItem_MouseLeftButtonDown中引用sp,按正统处理方法还真是比较麻烦(各位可以google,baidu印证),这里给出一个很取巧的办法:
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" x:Name="sp" Loaded="sp_Loaded"></StackPanel>
</ItemsPanelTemplate>
然后在后端代码中,添加一个私有变量,并处理sp_Loaded事件:
StackPanel _sp = null;
private void sp_Loaded(object sender, RoutedEventArgs e)
{
_sp = sender as StackPanel;
}
这样,在listItem_MouseLeftButtonDown中就能借助"_sp"正确引用到ItemsPanelTemplate中的sp了
对于应用绑定对象取得所选择Item的内容,,参照地址:http://www.oschina.net/question/213217_52095其内容如下
public class Person { public Person(string firstName, string lastName, int age) { FirstName = firstName; LastName = lastName; Age = age; } public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public override string ToString() { return LastName + ", " + FirstName + "{" + Age + "}"; } }
<phone:PhoneApplicationPage x:Class="DataboundMultiListBoxSelection.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" shell:SystemTray.IsVisible="True"> <!--LayoutRoot is the root grid where all page content is placed--> <Grid x:Name="LayoutRoot" Background="Transparent"> <ListBox x:Name="PeopleListBox" SelectionChanged="PeopleListBox_SelectionChanged"> <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="150" /> <ColumnDefinition Width="150" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Button Grid.Column="0" Content="{Binding FirstName}" Click="FirstNameButton_Click" /> <TextBlock Grid.Column="1" VerticalAlignment="Center" Text="{Binding LastName}" MouseLeftButtonUp="LastNameTextBlock_MouseLeftButtonUp" /> <Rectangle Grid.Column="2" Width="36" Height="36" Fill="Red" MouseLeftButtonUp="AgeRect_MouseLeftButtonUp" /> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </phone:PhoneApplicationPage>
public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { List<Person> people = new List<Person>(); people.Add(new Person("Tim", "Mgee", 36)); people.Add(new Person("Frank", "Solo", 77)); people.Add(new Person("Hanna", "Jones", 77)); PeopleListBox.ItemsSource = people; }
{ Person selectedPerson = ((sender as Button).DataContext as Person); MessageBox.Show("First Name Button clicked: " + selectedPerson.FirstName); }
private void LastNameTextBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { Person selectedPerson = ((sender as TextBlock).DataContext as Person); MessageBox.Show("Last Name TextBlock clicked: " + selectedPerson.LastName); e.Handled = true; } private void AgeRect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { Person selectedPerson = ((sender as Rectangle).DataContext as Person); MessageBox.Show("Age Rectangle clicked: " + selectedPerson.Age); e.Handled = true; }
private void PeopleListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (PeopleListBox.SelectedIndex == -1) return; Person selectedPerson = ((sender as ListBox).SelectedItem as Person); MessageBox.Show("ListBox selected: " + selectedPerson); PeopleListBox.SelectedIndex = -1; }