Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Assign a context to a UI Element

Assign a context to a UI Element

Scheduled Pinned Locked Moved WPF
questiondesignhelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Adrian0
    wrote on last edited by
    #1

    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!

    D P 2 Replies Last reply
    0
    • A Adrian0

      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!

      D Offline
      D Offline
      dasblinkenlight
      wrote on last edited by
      #2

      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
      }

      A 1 Reply Last reply
      0
      • D dasblinkenlight

        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
        }

        A Offline
        A Offline
        Adrian0
        wrote on last edited by
        #3

        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

        D 1 Reply Last reply
        0
        • A Adrian0

          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!

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          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

          My blog | My articles | MoXAML PowerToys | Onyx

          1 Reply Last reply
          0
          • A Adrian0

            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

            D Offline
            D Offline
            dasblinkenlight
            wrote on last edited by
            #5

            Yes, that trick ought to work! I verified that your code works with a slightly modified XAML (I used SilverLight 4):

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups