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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:14:08+00:00 2026-05-29T19:14:08+00:00

I want to set a controls datasource to an observable collection of key value

  • 0

I want to set a controls datasource to an observable collection of key value pairs, but to only display the key part of the pair.

I have a 3rd party multiselect combobox.

I have modified the datasource class that came with it so that it holds key value pair collections.

Here is the class:

public class DataSource : INotifyPropertyChanged
    {
        private ObservableCollection<KeyValuePair<string,string>> _items;

        public DataSource(ObservableCollection<KeyValuePair<string, string>> items)
        {
            _items = items;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion




        public ObservableCollection<KeyValuePair<string, string>> Items
        {
            get { return _items; }
        }

        private string _selectedItem = "";
        public string SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                OnPropertyChanged("SelectedItem");
            }
        }

        private ObservableCollection<KeyValuePair<string, string>> _selectedItems;
        public ObservableCollection<KeyValuePair<string, string>> SelectedItems
        {
            get
            {
                if (_selectedItems == null)
                {
                    _selectedItems = new ObservableCollection<KeyValuePair<string, string>> { new KeyValuePair<string,string>("ALL"," ") };
                    SelectedItemsText = WriteSelectedItemsString(_selectedItems);
                    _selectedItems.CollectionChanged +=
                        (s, e) =>
                        {
                            SelectedItemsText = WriteSelectedItemsString(_selectedItems);
                            OnPropertyChanged("SelectedItems");
                        };
                }
                return _selectedItems;
            }
            set
            {
                _selectedItems = value;
            }
        }

        public string SelectedItemsText
        {
            get { return _selectedItemsText; }
            set
            {
                _selectedItemsText = value;
                OnPropertyChanged("SelectedItemsText");
            }
        } string _selectedItemsText;


        private static string WriteSelectedItemsString(IList<KeyValuePair<string, string>> list)
        {
            if (list.Count == 0)
                return String.Empty;

            StringBuilder builder = new StringBuilder(list[0].Key);

            for (int i = 1; i < list.Count; i++)
            {
                builder.Append(", ");
                builder.Append(list[i].
);
            }

            return builder.ToString();
        }
    }

In the code behind I have:

DataSource ds = new DataSource(materialNames);
            cmbLastEditors.DataContext = ds;

And in my xaml I have:

<my1:MultiComboBox Name="cmbLastEditors"  Grid.Row="8" Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" SelectionMode="Multiple" DisplaySeparator=", " ItemsSource="{Binding Items}" SelectedItems="{Binding SelectedItems}"  />

I want to do something like

ItemsSource="{Binding Items.Key}"

Could anyone help me? Please let me know if you need anymore information.

  • 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-29T19:14:09+00:00Added an answer on May 29, 2026 at 7:14 pm

    For regular comboboxes you have the ValueMember and the DisplayMember field.
    Which works similar to DisplayMemberPath. See this link for a better explanation, but your ComboBox should look like this:

    <my1:MultiComboBox Name="cmbLastEditors"  
                       Grid.Row="8" Grid.Column="1" 
                       VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
                       SelectionMode="Multiple" DisplaySeparator=", " 
                       ItemsSource="{Binding Items}" 
                       SelectedItems="{Binding SelectedItems}" 
                       DisplayMemberPath="Key" />
    

    Of course I’m not sure if this works with your third party combobox.

    If that will not work, you can use a value converter in the ItemTemplate of the Combobox. For example add a converter to a textblock in the ItemTemplate, where you bind the whole object and use the converter to return the key value to display.

    class MyKeyValueConverter : IValueConverter
    {
        public object Convert(object aValue)
        {
            var pair = aValue as KeyValuePair<string, string>;
            return pair.Key;
        }
    }
    
    
    <TextBlock Text="{Binding Path=., Converter={StaticResource myKeyValueConverter}}"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have UserControls containing other controls. I want that if I set a Foreground
I want to loop through the controls on a UserControl and set a property
I have one control named thumbviewer inside repeater. I want to set its imageurl
I want set Listbox background to transparent but not working Is there any idea?
i want set class for repeater when doPostBack for those dataID i wan but
My question is : I have an EditText and I want set a margin
I create a combobox in a class and want to set the selected value
I have a collection of StackPanel which each one includes a dynamic set of
I have a combobox with DropDownStyle set to DropDown . I want it to
I'm using DevExpress LookUpEdit control in my application. I want to set the datasource

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.