日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Windows Phone DataBound ListBox中针对UIElement的事件绑定(Button)

發布時間:2025/5/22 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Windows Phone DataBound ListBox中针对UIElement的事件绑定(Button) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在很多使用DataBound的ListBox案例中,我們都監聽了它的 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添加數據。

?

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

運行程序,結果如下圖所示:

接著。為Button實現點擊事件的處理:

{Person selectedPerson = ((sender as Button).DataContext as Person);MessageBox.Show("First Name Button clicked: " + selectedPerson.FirstName); }

比較有意思的是我們這里首先將 sender 強制轉換成 Button, 然后獲取 Button.DataContext 屬性。問題來了,什么事 DataContext? 當你將數據綁定到ListBox時,每一項被分配一個 DataContext 數據來表示綁定的單項數據。在今天這個范例中,DataContext對應的是每一個Person。而且DataContext我們可以稱為路由屬性,在ListBoxItem的每一個子控件中都可以被獲取。所以這也是為什么Button.DataContext就是我們所需要的Person的原因。

好了,現在你點擊任何按鈕,比如點擊這個"Tim"按鈕,我們將能看到下圖: OK,現在來看看每一項的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 來標記該事件已經完成。

?

最后,我們看看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后將不會按照順序被觸發。即Selection不會被改變。那么我們將看到下圖這個樣子。

轉自:http://www.oschina.net/question/213217_52095

?

?

?

?

?

?

?

總結

以上是生活随笔為你收集整理的Windows Phone DataBound ListBox中针对UIElement的事件绑定(Button)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。