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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:33:21+00:00 2026-05-30T14:33:21+00:00

Is there a way to add a SortedList or a Dictionary to a ResourceDictionary

  • 0

Is there a way to add a SortedList or a Dictionary to a ResourceDictionary and use (and bind!) it to a control via XAML?

I’ve tried this, but I couldn’t figure out how to do it:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:coll="clr-namespace:System.Collections.Generic;assembly=mscorlib">

    <x:Array x:Key="test"
             Type="sys:Object">
        <coll:KeyValuePair>***</coll:KeyValuePair>
    </x:Array>
  • 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-05-30T14:33:22+00:00Added an answer on May 30, 2026 at 2:33 pm

    SortedList is easy as it is not generic.

    If a class implements IDictionary you can add values by defining them as the child nodes using x:Key to set the key by which they should be added to the dictionary.

    xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
    
    <col:SortedList x:Key="list">
        <sys:String x:Key="0">Lorem</sys:String>
        <sys:String x:Key="1">Ipsum</sys:String>
        <sys:String x:Key="2">Dolor</sys:String>
        <sys:String x:Key="3">Sit</sys:String>
    </col:SortedList>
    
    <!-- Usage: -->
    <ContentControl Content="{Binding [0], Source={StaticResource list}}" />
    

    The item keys are strings here, to get actual ints you could use a custom markup extension which parses the string to int, or by defining the keys as resource first:

    <sys:Int32 x:Key="key1">0</sys:Int32>
    <sys:Int32 x:Key="key2">1</sys:Int32>
    <sys:Int32 x:Key="key3">2</sys:Int32>
    <sys:Int32 x:Key="key4">3</sys:Int32>
    
    <col:SortedList x:Key="list">
        <sys:String x:Key="{StaticResource key1}">Lorem</sys:String>
        <sys:String x:Key="{StaticResource key2}">Ipsum</sys:String>
        <sys:String x:Key="{StaticResource key3}">Dolor</sys:String>
        <sys:String x:Key="{StaticResource key4}">Sit</sys:String>
    </col:SortedList>
    

    The binding then becomes more complex as the indexer value needs to be cast to int explicitly as it otherwise would be interpreted as string.

    <ContentControl Content="{Binding Path=[(sys:Int32)0],
                                      Source={StaticResource list}}"/>
    

    You cannot omit the Path= because of an implementation detail.


    Dictionaries are not so easy because they are generic and there (currently) is no simple built-in way to create generic objects in XAML. Using markup extensions however you can create generic objects via reflection.

    Implementing IDictionary on such an extension also allows you to fill that newly created instance. Here is a very sketchy example:

    public class DictionaryFactoryExtension : MarkupExtension, IDictionary
    {
        public Type KeyType { get; set; }
        public Type ValueType { get; set; }
    
        private IDictionary _dictionary;
        private IDictionary Dictionary
        {
            get
            {
                if (_dictionary == null)
                {
                    var type = typeof(Dictionary<,>);
                    var dictType = type.MakeGenericType(KeyType, ValueType);
                    _dictionary = (IDictionary)Activator.CreateInstance(dictType);
                }
                return _dictionary;
            }
        }
    
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return Dictionary;
        }
    
        public void Add(object key, object value)
        {
            if (!KeyType.IsAssignableFrom(key.GetType()))
                key = TypeDescriptor.GetConverter(KeyType).ConvertFrom(key);
            Dictionary.Add(key, value);
        }
    
        #region Other Interface Members
        public void Clear()
        {
            throw new NotSupportedException();
        }
        public bool Contains(object key)
        {
            throw new NotSupportedException();
        }
        // <Many more members that do not matter one bit...>
        #endregion
    }
    
    <me:DictionaryFactory x:Key="dict" KeyType="sys:Int32" ValueType="sys:String">
        <sys:String x:Key="0">Lorem</sys:String>
        <sys:String x:Key="1">Ipsum</sys:String>
        <sys:String x:Key="2">Dolor</sys:String>
        <sys:String x:Key="3">Sit</sys:String>
    </me:DictionaryFactory>
    

    As passing in a typed instance as key is a bit of a pain i chose to do the conversion in IDictionary.Add before the value is added to the internal dictionary instead (this may cause problems with certain types).

    Since the dictionary itself is typed the binding should not require a cast.

    <ContentControl Content="{Binding [0], Source={StaticResource dict}}" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to add a resource to a ResourceDictionary from code without
Is there any way to add a SQL Server Database Diagram to source control?
Is there a way to add ImageViews to a PreferenceCategory? How? I know this
is there a way to add a user control to a WPF Window created
Is there a way/add-on that I can use so everytime any javascript function is
Is there a way to add class by default in a link_to helper? This
Is there any way to add a window form into another form as control?
Is there a way to add events to a control like adding properties to
Is there a way add another dropdown form to this script? http://javascript.internet.com/navigation/connected-comboxes.html Or do
is there a way to add an existing classic ASP webapp into a solution

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.