Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9215869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:17:54+00:00 2026-06-18T02:17:54+00:00

I have a Dictionary, which contains data like so: key Value UK01 Building 1

  • 0

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 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:17:55+00:00Added an answer on June 18, 2026 at 2:17 am

    You need to use a DataTemplate in your ListBox to ensure the listbox knows how to render it’s content. Assuming you follow your class based approach as above:

    <ListBox>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding value}">
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    This creates a DataTemplate for each item you have added to the ListBox which contains a text block which is bound to a property on the items added

    Read 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:

    public string ID { get; set; }
    

    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.

    var myList = new ObservableCollection<MyClass>();
    foreach(var kvp in dict) 
    {
        myList.Add(new MyClass(kvp.Key, kvp.Value)); 
        // Or myList.Add(new MyClass() { ID = kvp.Key, Value = kvp.Value }); depending on your constructor
    }
    

    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:

    <ListBox>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Key}">
                    <TextBlock Text="{Binding Value}">
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    Should give you

    {Key}
    {Value}
    {Key}
    {Value}
    {Key}
    {Value}
    

    In your listbox

    Assuming you don’t need any additional functionality from the result items KeyValuePair should 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/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have file which contains some data, like this 2011-01-02 100100 1 2011-01-02 100200
I have a list which contains a dictionary of ExpandoObjects. Im binding this to
I have a class which contains a generic dictionary: protected Dictionary<K,T> Data { get;
I have a list which contains dictionaries. Each dictionary has an attribute description which
I have a simple text dictionary file, which contains words, separated by ';'.My problem
I have 2 dictionary which contain the same keys but the value pairs are
I have a dictionary object which i would like to encrypt, then put it
I have a parent contentcontrol which displays data via a datatemplate. The datatemplate contains
I have a Dictionary> in c# Dictionary<string,List<string>> l_dictRawData; which contains the values are :
I have a data in format: id1 id2 value Something like 1 234 0.2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.