Thursday, December 20, 2012

How To select and object of label , If it is a child of Grid in C#

How To select and object of label , If it is a child of Grid in C#

I have a program in which I have to change text of label (on the click of button) which is a child of a grid

public class XLabel {  Grid uiGrid = null;  TextBlock textblock = null;  string emptyString = "";   Public void createLabel()  {   uiGrid.Children.Add(textblock);      grid.Children.Add(uiGrid);       }   public void cleartext()  {                   textblock.Text = emptyString;            } } 

In other class I have a method to clear text

public void clearText() {            XLabel obj = new XLabel();   obj.cleartext(indexi);           } 

How to select specific label to clear text from specific grid if there are many grids and each having one label .

Answers & Comments...

Answer: 1

The Grid object has properties like Name or Tag, that can be used for searching.

If you create grids programmatically, you should create a unique property for each, then in your clearText method you just receive all Grid objects from XLabel object and search for the one with proper name/tag.


To get a list of labels from grid, you could use lambda like that:

List<UIElement> list =            YourGrid.Children.Where(o => o.GetType() == typeof(Label)).ToList(); 
by : Olterhttp://stackoverflow.com/users/1115918

Answer: 2

To extend Olter's answer,

Create your Textblock and Grid like this

Grid uiGrid = new Grid() { Name = "uiGrid"+1 }; TextBlock textblock = new TextBlock() { Name = "textBlock"+1 }; 

Each time change the number you add to the grid and textblock and somehow plan to keeptrack of that number.

Then when you want to clear the text,

(this.FindName("textBlock"+1) as TextBlock).Text = ""; 
by : nkchandrahttp://stackoverflow.com/users/649306




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog