0

I'm writing a card game (think on Magic for example) where I have 8 cards on the table all the time. Each card has 4 textboxes (name, skill A, skill B, description) and 2 images (mana and photo). I want to be able to access programmatically to any field at any time, for example, skill A on card #5.

I can't use the control name because I'll be accessing to Card5_SkillA, which is very inefficient.

I've tried grouping the controls inside a Grid, and setting the Tag field both at runtime and compiletime to "Skill_A" and try to find it like this:

 foreach (UIElement c in mygrid.Children)
    {
        if ( c is TextBox && ((TextBox) c).Tag.ToString() == "Skill_A")
            System.Windows.Forms.MessageBox.Show("you found me");
    }

But when executing that loop the Tag field is always null.

I've searched others questions but TreeHelper seems overkill and I can't search by row/column because I want to be able to redesign the card dessign in the future without modifying the code.

Any advice? Thanks =)

Guillermo Mestre
  • 490
  • 3
  • 16
  • Take a look at MVVM design pattern, that makes live a lot easier – Flat Eric May 27 '14 at 19:42
  • You don't "find a specific control in a Grid" in WPF. You use proper DataBinding and create a proper ViewModel and use a proper ItemsControl instead. And BTW, remove that reference to `System.Windows.Forms.dll`, WPF does not care about that. – Federico Berasategui May 27 '14 at 19:45
  • Hum DataBinding, I'll search that and try to use it =) What do you mean by removing the reference? – Guillermo Mestre May 27 '14 at 19:52

1 Answers1

0

You do not want to search for data through your controls. Controls are there to display and use data and not to structure it. Instead of that, take a look at the MVVM pattern: MVVM: Tutorial from start to finish?

Create a ViewModel which will be your Game. Have your UI bind to that ViewModel and have its controls bind to the properties of the Game. That way you have your game logic decoupled from the UI logic and completely structured.

Community
  • 1
  • 1
Edin
  • 1,476
  • 11
  • 21