Windows Phone DataBound ListBox中针对UIElement的事件绑定(Button)
在很多使用DataBound的ListBox案例中,我們都監(jiān)聽了它的 SelectionChanged 事件,當我們用手指點擊某一項時,可以從 ListBox.SelectedItem 屬性上很容易獲得這個被點擊的對象。然而,萬一你的ListBox的單項里面有很多類似于 Button, TextBlock 這樣的控件,而你剛好又要捕獲這些控件的點擊事件時,那你該這么做?不過我將在下面的文章中談談一個簡單的解決方法
?
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 + "}";} }接著來寫XAML布局代碼,先添加一個3列的ListBox,如下代碼所示。
?
<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>然后在C#后臺代碼中,我們在Page_Load事件中為ListBox添加數(shù)據(jù)。
?
public partial class MainPage : PhoneApplicationPage {// Constructorpublic 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;}運行程序,結(jié)果如下圖所示:
接著。為Button實現(xiàn)點擊事件的處理:
{Person selectedPerson = ((sender as Button).DataContext as Person);MessageBox.Show("First Name Button clicked: " + selectedPerson.FirstName); }比較有意思的是我們這里首先將 sender 強制轉(zhuǎn)換成 Button, 然后獲取 Button.DataContext 屬性。問題來了,什么事 DataContext? 當你將數(shù)據(jù)綁定到ListBox時,每一項被分配一個 DataContext 數(shù)據(jù)來表示綁定的單項數(shù)據(jù)。在今天這個范例中,DataContext對應的是每一個Person。而且DataContext我們可以稱為路由屬性,在ListBoxItem的每一個子控件中都可以被獲取。所以這也是為什么Button.DataContext就是我們所需要的Person的原因。
好了,現(xiàn)在你點擊任何按鈕,比如點擊這個"Tim"按鈕,我們將能看到下圖: OK,現(xiàn)在來看看每一項的TextBlock以及Rectangle被點擊后的事件處理: 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; }注意我們這里設置 Handled 屬性為true,這一點與Button的點擊事件處理可不一樣,因為 MouseLeftButtonUp 事件如果不手動停止的話,會無限路由下去。所以當然要設置 Handled = true 來標記該事件已經(jīng)完成。
?
最后,我們看看ListBox.SelectionChanged事件的做法。這里的處理是捕獲所有的沒有點擊到按鈕,TextBlock以及Rectangle的點擊事件。
?
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; }注意上面代碼一開始去檢測 SelectedIndex 是否為 -1 ,而且最后還強行設置 SelectedIndex = -1,如果你不這樣做,那么SelectionChanged事件在你觸碰到ListBox后將不會按照順序被觸發(fā)。即Selection不會被改變。那么我們將看到下圖這個樣子。
轉(zhuǎn)自:http://www.oschina.net/question/213217_52095
?
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的Windows Phone DataBound ListBox中针对UIElement的事件绑定(Button)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 想找一本类似于pass绿卡图书的学霸笔记
- 下一篇: 接口线程安全隐患