Assign a context to a UI Element
-
I have several pairs of TextBlock and an Image next to each other. OnMouseLeftButtonDown I want to modify the TextBlock. That is some standard situation, but in this case I don't want to add an EventHandler to each image as there are many of them. How can I user only one Handler and change the corresponding TextBlock. I've tried to add some DataContext to the image but it did not work out. Can anyone give me some help? Thanks!
-
I have several pairs of TextBlock and an Image next to each other. OnMouseLeftButtonDown I want to modify the TextBlock. That is some standard situation, but in this case I don't want to add an EventHandler to each image as there are many of them. How can I user only one Handler and change the corresponding TextBlock. I've tried to add some DataContext to the image but it did not work out. Can anyone give me some help? Thanks!
You can use the
Tag
tag to store additional info with your images, for example:<Image Tag="textbox1" MouseLeftButtonDown="Image_MouseLeftButtonDown" />
<Image Tag="textbox2" MouseLeftButtonDown="Image_MouseLeftButtonDown" />Now you can access the tag in your event handler, and act based on its value:
private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
var textboxName = ((Image) sender).Tag;
// pick a text box based on the tag
} -
You can use the
Tag
tag to store additional info with your images, for example:<Image Tag="textbox1" MouseLeftButtonDown="Image_MouseLeftButtonDown" />
<Image Tag="textbox2" MouseLeftButtonDown="Image_MouseLeftButtonDown" />Now you can access the tag in your event handler, and act based on its value:
private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
var textboxName = ((Image) sender).Tag;
// pick a text box based on the tag
}Hi dasblinkenlight, thank you for your reply. This might be a solution. Would be great to access the textbox directly when an event is fired. Otherwise I need to put all textboxes in an array or not? I thought of something like this below.
<TextBox x:Name="UserNameTB" />
<TextBox x:Name="AddressTB" />
<Image Source="..." MouseLeftButtonDown="editText_MouseLeftButtonDown" Tag="{UserNameTB}"/>
<Image Source="..." MouseLeftButtonDown="editText_MouseLeftButtonDown" Tag="{AddressTB}"/>Is it possible to access it like this??
TextBox userNameTB = ((sender as Image).Tag) as TextBox);
Tried some combinations but with no luck. Greets Adrian
-
I have several pairs of TextBlock and an Image next to each other. OnMouseLeftButtonDown I want to modify the TextBlock. That is some standard situation, but in this case I don't want to add an EventHandler to each image as there are many of them. How can I user only one Handler and change the corresponding TextBlock. I've tried to add some DataContext to the image but it did not work out. Can anyone give me some help? Thanks!
If I were doing this, I'd have a collection containing the details of the image, and a text item representing the text you want to display in the TextBlock. Then, I'd bind to this using something like a ListView. In other words, I'd use MVVM to cope with this. I suggest that you have a look into this, and simplify the problem hugely.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
Hi dasblinkenlight, thank you for your reply. This might be a solution. Would be great to access the textbox directly when an event is fired. Otherwise I need to put all textboxes in an array or not? I thought of something like this below.
<TextBox x:Name="UserNameTB" />
<TextBox x:Name="AddressTB" />
<Image Source="..." MouseLeftButtonDown="editText_MouseLeftButtonDown" Tag="{UserNameTB}"/>
<Image Source="..." MouseLeftButtonDown="editText_MouseLeftButtonDown" Tag="{AddressTB}"/>Is it possible to access it like this??
TextBox userNameTB = ((sender as Image).Tag) as TextBox);
Tried some combinations but with no luck. Greets Adrian
Yes, that trick ought to work! I verified that your code works with a slightly modified XAML (I used SilverLight 4):