I have a Dictionary, which contains data like so:
key Value
UK01 Building 1
UK02 Building 2
I have a textbox in which data is populated like this:
foreach (var building in dictionary)
{
listbox1.Items.Add(building.Value);
}
This then displays Building 1 Building 2, ...
Problem:
What I want to do is so that when the SelectionChanged is triggered, I can access the Key/Value for the option that has been selected and store these as variables which I can use later on. Currently I can only get the item selected, i.e “Building 1” or I can get the SelectedIndedbut this only gives me: 1, 2, .. And I understand why.
Is it therefore possible (elegantly) so that the key/value can be accessed without displaying the key? I have tried to use a class:
public class Test
{
public ID { get; set; }
public value { get; set; }
}
But this did not work. Does anyone have any suggestions?
Thank you 🙂
You need to use a
DataTemplatein yourListBoxto ensure the listbox knows how to render it’s content. Assuming you follow your class based approach as above:This creates a
DataTemplatefor each item you have added to theListBoxwhich contains a text block which is bound to a property on the items addedRead up on binding – it is one of the most powerful things in WPF/WP7/Silverlight and probably one of the most extensive implementations of binding out there
http://www.mono-software.com/blog/post/Mono/166/Data-binding-in-Windows-Phone-7-application/
Edit:
Though I’m pretty sure the default template for ListBox is just to call ‘ToString()’ on each item… so it should be working anyway!
Oh and consider making the members auto-properties instead of fields:
Edit:
Ok I’m not great with Linq but since the query should return an
IEnumerable<KeyValuePair<string, string>>as far as I can see, you can just enumerate it and build your objects off it e.g.Having said this, there no reason why you can’t just point to the key/value of the dictionary items (
KeyValuePair<string, string>is just a reference type like anything else)This:
Should give you
In your listbox
Assuming you don’t need any additional functionality from the result items
KeyValuePairshould suffice. Creating a class just to show some values may be overkill since you are already 99% of the way there.You might want to look at some MVVM patterns and maybe get into looking at an MVVM framework (I’m biased because I use it all the time but Caliburn.Micro is really easy to get started with and supports WP7).
Your code wouldn’t be any more complex, but certain things would be wired up for you and it would give you more feedback as to where you are going wrong with bindings etc (some bindings are hard to figure out especially when popups/contextmenus are involved)
If you are serious about developing for XAML based technologies, they go hand in hand with the MVVM pattern, and a framework just makes things a cinch
If you are interested there’s an easy to follow tut here:
https://caliburnmicro.codeplex.com/wikipage?title=Basic%20Configuration%2c%20Actions%20and%20Conventions&referringTitle=Documentation
Another variation on the same tut with a bit more info is here:
http://buksbaum.us/2010/08/01/caliburn-micro-hello-world/