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