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. Retrieve content of a Cell in WPF Grid

Retrieve content of a Cell in WPF Grid

Scheduled Pinned Locked Moved WPF
csharpcsswpftutorialquestion
4 Posts 2 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.
  • E Offline
    E Offline
    ezazazel
    wrote on last edited by
    #1

    Hi again! WPF again.. [sight] I have a simple WPF grid. Furthermore I have two different types of items in this grid, a self defined which inherits from TextBlock and contains PositionX and PositionY - these are my main controls; and TextBlocks whcih do the role of column and rowheaders --------------- |0|1|2|3|4|5|6| |1|_|_|_|_|_|_| |2|_|_|_|_|_|_| |3|_|X|_|_|_|_| --------------- So I click on x and with e.Originalsource casted to my Class i get the information inside my object. What I need now are the Tooltips of the two headers, in this case Tooltip of Textblock in cell 2/0 and the ToolTip of cell 0/3. As mentioned before those are Type of TextBlock. A foreach iteration does not work, as the Headers are created this way:

    for (int i = 0; i < 65; i++)
    {
    TextBlock b1 = new TextBlock() { Text = i.ToString(), ToolTip = GetInformationFromXML(i, 0) };
    Grid.SetColumn(b1, 0);
    Grid.SetRow(b1, i);
    g.Children.Add(b1);

                if (i != 0)
                {
                    TextBlock b2 = new TextBlock() { Text = i.ToString(), ToolTip = GetInformationFromXML(i, 0) };
                    Grid.SetColumn(b2, i);
                    Grid.SetRow(b2, 0);
                    g.Children.Add(b2);
                }
            }
    

    So there is no unique name at all. What I get is

    MyObject o = (MyObject)e.OriginalSource;
    int X = o.GridPositionX;
    int Y = o.GridPositionY;

    What I need is

    TextBlock tb1 = (TextBlock)grid.GetItem(X,0);
    TextBlock tb2 = (TextBlock)grid.GetItem(0,Y);

    grid[X][0] does not work. Can someone please explain me how to retrieve these? Thank you in advance, eza

    M 1 Reply Last reply
    0
    • E ezazazel

      Hi again! WPF again.. [sight] I have a simple WPF grid. Furthermore I have two different types of items in this grid, a self defined which inherits from TextBlock and contains PositionX and PositionY - these are my main controls; and TextBlocks whcih do the role of column and rowheaders --------------- |0|1|2|3|4|5|6| |1|_|_|_|_|_|_| |2|_|_|_|_|_|_| |3|_|X|_|_|_|_| --------------- So I click on x and with e.Originalsource casted to my Class i get the information inside my object. What I need now are the Tooltips of the two headers, in this case Tooltip of Textblock in cell 2/0 and the ToolTip of cell 0/3. As mentioned before those are Type of TextBlock. A foreach iteration does not work, as the Headers are created this way:

      for (int i = 0; i < 65; i++)
      {
      TextBlock b1 = new TextBlock() { Text = i.ToString(), ToolTip = GetInformationFromXML(i, 0) };
      Grid.SetColumn(b1, 0);
      Grid.SetRow(b1, i);
      g.Children.Add(b1);

                  if (i != 0)
                  {
                      TextBlock b2 = new TextBlock() { Text = i.ToString(), ToolTip = GetInformationFromXML(i, 0) };
                      Grid.SetColumn(b2, i);
                      Grid.SetRow(b2, 0);
                      g.Children.Add(b2);
                  }
              }
      

      So there is no unique name at all. What I get is

      MyObject o = (MyObject)e.OriginalSource;
      int X = o.GridPositionX;
      int Y = o.GridPositionY;

      What I need is

      TextBlock tb1 = (TextBlock)grid.GetItem(X,0);
      TextBlock tb2 = (TextBlock)grid.GetItem(0,Y);

      grid[X][0] does not work. Can someone please explain me how to retrieve these? Thank you in advance, eza

      M Offline
      M Offline
      MIHAI_MTZ
      wrote on last edited by
      #2

      What you are doing is quite strange ... it's not a good idea to keep important data in the tooltip property.

      private UIElement getElement(int column, int row)
              {
                  foreach (UIElement e in MyGrid.Children)
                      if (Grid.GetColumn(e) == column)
                          if (Grid.GetRow(e) == row)
                              return e;
      
                  return null;
              }
      

      You will have to cast the UIElement to textblock to get the tooltip.

      E 1 Reply Last reply
      0
      • M MIHAI_MTZ

        What you are doing is quite strange ... it's not a good idea to keep important data in the tooltip property.

        private UIElement getElement(int column, int row)
                {
                    foreach (UIElement e in MyGrid.Children)
                        if (Grid.GetColumn(e) == column)
                            if (Grid.GetRow(e) == row)
                                return e;
        
                    return null;
                }
        

        You will have to cast the UIElement to textblock to get the tooltip.

        E Offline
        E Offline
        ezazazel
        wrote on last edited by
        #3

        Thank you for your solution. Well, concearnig the data in the tooltip: this project represents a 128x128 matrix, so I cannot use full names for the headers - you couldn't read them any more. So I thought I pass the full name to the tooltip and retrieve them when hoovering over an item. the item generates a tooltip which shows the full name of the row and the full name of the column. The names of the single devices (a.ka headers) are gnereated out of an xml file, so I don't want to open/read/close it every time I create an item (which represents a line between two devices). Either I don't think it's necessary to hold the items in a Dictionary, just for this. I could use the Tag Property instead, anyway I just need the Text (the full name) of the device. Do you have any better ideas how this can be done? I would be more than happy to hear them. Thank you, eza

        M 1 Reply Last reply
        0
        • E ezazazel

          Thank you for your solution. Well, concearnig the data in the tooltip: this project represents a 128x128 matrix, so I cannot use full names for the headers - you couldn't read them any more. So I thought I pass the full name to the tooltip and retrieve them when hoovering over an item. the item generates a tooltip which shows the full name of the row and the full name of the column. The names of the single devices (a.ka headers) are gnereated out of an xml file, so I don't want to open/read/close it every time I create an item (which represents a line between two devices). Either I don't think it's necessary to hold the items in a Dictionary, just for this. I could use the Tag Property instead, anyway I just need the Text (the full name) of the device. Do you have any better ideas how this can be done? I would be more than happy to hear them. Thank you, eza

          M Offline
          M Offline
          MIHAI_MTZ
          wrote on last edited by
          #4

          It's nothing wrong with what you did. I'd still use the Tag property because it can handle any object and not just strings and because you never know when you might need the tooltip property for a real tooltip.

          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