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 8966639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:03:55+00:00 2026-06-15T17:03:55+00:00

I am using the MVVM model with a dynamic field generator, where the field

  • 0

I am using the MVVM model with a dynamic field generator, where the field is pulled from the database, done this way because different types of forms require different fields (TextBox/TextBlock, ComboBox, etc.). The problem is I’m trying to retrieve a value from a dictionary, to display in a TextBlock for the form, but I’m not sure how to bind the retrieved Key so I can display the value.

Currently, I am doing the following:

 TextBlock textBlock = new TextBlock();
 textBlock.SetBinding(TextBlock.TextProperty, createFieldBinding(myPropertyName);

With the following binding method:

 private Binding createFieldBinding(string fieldName) {
      Binding binding = new Binding(fieldName);
      binding.Source = this.DataContext;
      binding.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
      return binding;
 }

Where I pass something through like Score, which maps to a Score property in the ViewModel, but how would I bind to a Dictionary Key to retrieve its Value?

I want to be able to bind to something like myDictionaryProperty[myDictionaryKey], if that is possible.

Example:
The below generates the PlayerScore for Player with ID of 1.
Where PlayerScore is a Dictionary<int, int> and PlayerID is an int.

 <TextBlock Name="textBlockA" Text="{Binding PlayerScore[1]} />
  • 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-15T17:03:56+00:00Added an answer on June 15, 2026 at 5:03 pm

    Using this solution provided by @Clemens, I was able to build my own DictionaryItemConverter, based on the data types for my Dictionary, and create a multi-binding method that would bind the Key and the Dictionary together.

    Converter:

     public class DictionaryItemConverter : IMultiValueConverter {
          public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
               if(values != null && values.Length >= 2) {
                    var myDict = values[0] as IDictionary;
                    if(values[1] is string) {
                         var myKey = values[1] as string;
                         if(myDict != null && myKey != null) {
                              //the automatic conversion from Uri to string doesn't work
                              //return myDict[myKey];
                              return myDict[myKey].ToString();
                         }
                    }
                    else {
                         long? myKey = values[1] as long?;
                         if(myDict != null && myKey != null) {
                              //the automatic conversion from Uri to string doesn't work
                              //return myDict[myKey];
                              return myDict[myKey].ToString();
                         }
                    }
               }
    
               return Binding.DoNothing;
          }
    
          public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) {
               throw new NotSupportedException();
          }
     }
    

    Multi-Bind Method:

     private MultiBinding createFieldMultiBinding(string fieldName) {
          // Create the multi-binding
          MultiBinding mbBinding = new MultiBinding();
          // Create the dictionary binding
          Binding bDictionary = new Binding(fieldName + "List");
          bDictionary.Source = this.DataContext;
          // Create the key binding
          Binding bKey = new Binding(fieldName);
          bKey.Source = this.DataContext;
          // Set the multi-binding converter
          mbBinding.Converter = new DictionaryItemConverter();
          // Add the bindings to the multi-binding
          mbBinding.Bindings.Add(bDictionary);
          mbBinding.Bindings.Add(bKey);
    
          return mbBinding;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this Silverlight Prism application that is using MVVM. The model calls a
Using MVVM, one type of ViewModels include the Model they represnt as a Field.
I'm starting to creat this project using MVVM Model. But I have no idea
Does using MVVM model in WPF restricts the programmer from writing code behind? Avoiding
I am using the MVVM model. I have a dependency property, a boolean, called
I am using MVVM architecture. I have a usercontrol UC as a View Model
I am building a MVVM application. The model / entity (I am using NHibernate)
Using MVVM and EF...I have a datagrid binding to a View Model (using ObservableCollection).
I'm using MVVM (MVVM Light Toolkit) and have a property on the view model
I am building a WPF 4.0 Application using MVVM. The Model is generated using

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.