Created
May 14, 2013 01:37
-
-
Save kimsk/5572989 to your computer and use it in GitHub Desktop.
เราจะได้ sender จาก PointerEventHandler ซึ่งเราสามารถนำมันมาหา index ได้ครับ ในตัวอย่างผมแสดงสามวิธีง่ายๆ:
1. เราอาจจะใช้ DataContext
2. หรือใช้ name หรือ ค่าอื่นๆ
3. หา index จาก array ที่เก็บ sender
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed partial class MainPage : Page | |
{ | |
UIElement[] _uiElements; | |
public MainPage() | |
{ | |
this.InitializeComponent(); | |
var uiElements = new List<UIElement>(); | |
for (int i = 0; i < 10; i++) | |
{ | |
var txtBlock = new TextBlock | |
{ | |
FontSize = 20.0d, | |
Text = "TEXT "+i, | |
DataContext = i, | |
Name = "txtBlock-" + i, | |
}; | |
txtBlock.PointerPressed += txtBlock_PointerPressed; | |
stackPanel.Children.Add(txtBlock); | |
uiElements.Add(txtBlock); | |
} | |
_uiElements = uiElements.ToArray(); | |
} | |
void txtBlock_PointerPressed(object sender, PointerRoutedEventArgs e) | |
{ | |
// get from data context | |
int? txtBlockNum = ((FrameworkElement)sender).DataContext as int?; | |
// get from name | |
string txtBlockName = ((FrameworkElement)sender).Name; | |
string txtBlockNum2 = txtBlockName.Substring(txtBlockName.IndexOf("-")+1); | |
// get from _uiElements array | |
int txtBlockNum3 = Array.FindIndex(_uiElements, txtBlck => txtBlck == sender); | |
txtBox.Text = txtBlockNum.Value.ToString() + " " | |
+ txtBlockNum2 + " " | |
+ txtBlockNum3; | |
} | |
/// <summary> | |
/// Invoked when this page is about to be displayed in a Frame. | |
/// </summary> | |
/// <param name="e">Event data that describes how this page was reached. The Parameter | |
/// property is typically used to configure the page.</param> | |
protected override void OnNavigatedTo(NavigationEventArgs e) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment