Monday, March 3, 2014

WPF and Child Code...


Just a quick post because you never know who might need to do something like this as well (and maybe this can save them a few days of trying stuff/searching for different things/implementing things that don't work...)

So while making a tool for my internship, I had to implement a UI that would display data in a parent/child relationship. Ok, shouldn't be to bad, but the thing is that it could also have the same parent data in a list as well.

After trying for a while to get this setup to work I finally got something that displayed correctly by just getting the object and stripping the strings from it and displaying that and then more or less "discarding" the object (not discarding but not saving it in my UI for retrieval)

Then I was told we would be able to get things back by the unique ID that they would have (which also put me in a pickle because I wasn't using a Unique ID in my test data...Where I thought this might save me, it put me deeper into the, "Oh crap, how am I going to get the object back and get that ID?"

This is where StackOverflow came in to save the day...finally...after a week of searching different questions on there haha.

This answer is the answer that finally gave me the puzzle piece to get my Data Object into the UI as well as being able to get it back after (instead of just getting the strings and displaying that.)

Hopefully someone can find this helpful cause man that took way to long to get that to work..I must have tried over 15 different ways to do the datatemplates and none of them worked till this one so yeah.


One thing to note as well, when doing the data templates, if you're doing it in a treeview like I am/the example as well, you don't want to put a TreeViewItem into the datatemplate if you want to be able to click on the text to get selection.  I didn't realize I had done this till yesterday and was trying to figure out why you had to click on this tiny spot between the > and the text.  Apparently TreeView automatically wraps whatever you put in the template in a TreeViewItem anyways so you can just display with textblock/label etc. etc.