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

The Archive Base Latest Questions

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

I am new to WPF, C# and data binding. I am just trying to

  • 0

I am new to WPF, C# and data binding. I am just trying to bind a simple combo box to an ObservedCollection. Here is the code:

 public class Directory
{
    private string _ikey;
    public string IKey
    {
        get
        {
            return _ikey;
        }
        set
        {
            _ikey = value;
        }
    }
    private string _ivalue;
    public string IValue
    {
        get
        {
            return _ivalue;
        }
        set
        {
            _ivalue = value;
        }
    }

}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window

{
    public ObservableCollection<Directory> DirectoryList = new ObservableCollection<Directory>();

    public MainWindow()
    {
        InitializeComponent();

        DirectoryList = new ObservableCollection<Directory>();
        Directory _dirtemp = new Directory();
        _dirtemp.IKey = "1";
        _dirtemp.IValue = "Steve";
        DirectoryList.Add(_dirtemp);

        _dirtemp = new Directory();
        _dirtemp.IKey = "2";
        _dirtemp.IValue = "John";
        DirectoryList.Add(_dirtemp);




    }
}

My xaml looks like this:

<Window x:Class="DataBindCombo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DataBindCombo"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ComboBox Height="48" HorizontalAlignment="Left" Margin="70,104,0,0" Name="comboBox1" VerticalAlignment="Top" Width="310"
              ItemsSource="{Binding Path=DirectoryList}"
              DisplayMemberPath="IValue"
              SelectedValuePath="IKey"
              >

It seems like it should be simple. I was able to put the binding in the code behind and it worked fine, but I would like to have it bind through xaml.

Any ideas? TIA

  • 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-21T14:07:25+00:00Added an answer on May 21, 2026 at 2:07 pm

    There are a couple ways you can go about this. The basics are you need to make it so the XAML can see your collection. You can do this in an implicit manner by setting it to your DataContext. If this is the only thing you are binding then its a quick and dirty way of binding. It would look like this:

    public partial class MainWindow : Window
    
    {
        public ObservableCollection<Directory> DirectoryList;
    
        public MainWindow()
        {
            InitializeComponent();
    
            DirectoryList = new ObservableCollection<Directory>();
            Directory _dirtemp = new Directory();
            _dirtemp.IKey = "1";
            _dirtemp.IValue = "Steve";
            DirectoryList.Add(_dirtemp);
    
            _dirtemp = new Directory();
            _dirtemp.IKey = "2";
            _dirtemp.IValue = "John";
            DirectoryList.Add(_dirtemp);
    
    
            DataContext=DirectoryList;
    
        }
    }
    
    Window x:Class="DataBindCombo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DataBindCombo"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox Height="48" HorizontalAlignment="Left" Margin="70,104,0,0" Name="comboBox1" VerticalAlignment="Top" Width="310"
                  ItemsSource="{Binding}"
                  DisplayMemberPath="IValue"
                  SelectedValuePath="IKey"
                  >
    

    The other way is more sophisticated but probably what you will use more often. To do it You need to expose your collection in your MainWindow as a DependencyProperty and then bind to that value. It would look something like this:

    public partial class MainWindow : Window
        {
            public static DependencyProperty DirectoryListProperty =
                DependencyProperty.Register("DirectoryList",
                typeof(ObservableCollection<Directory>),
                typeof(MainWindow));
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            public ObservableCollection<Directory> DirectoryList
            {
                get { return (ObservableCollection<Directory>)base.GetValue(DirectoryListProperty); }
                set { base.SetValue(DirectoryListProperty, value); }
            }
        }
    
    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" x:Name="mainWindow">
        <Grid>
            <ComboBox Height="48" HorizontalAlignment="Left" Margin="70,104,0,0" Name="comboBox1" VerticalAlignment="Top" Width="310"
                  ItemsSource=" {Binding ElementName=mainWindow, Path=DirectoryList}"
                  DisplayMemberPath="IValue"
                  SelectedValuePath="IKey"
                  />
    

    This is also not the only way to accomplish it in this manner. In general rather than creating the list directly on the control you would create a view model. The MVVM pattern is the recommend way of creating your presentation, but my examples give you a way of getting functionality out there. You can play and experiment with different ways of doing this. I’ve discovered there are always multiple ways of doing things in WPF and its a matter of finding the one that fits the situation the best.

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

Sidebar

Related Questions

I am just starting out with WPF and am trying to understand data binding.
I'm new to WPF and data binding so hopefully I can explain the problem
I'm VERY new to WPF, and still trying to wrap my head around binding
I'm kind of new to WPF, and I'm trying to do some specialized data
I'm new to WPF. I have been been playing around with binding data to
I started a new WPF project in VS2008 and then added some code to
I'm considering integrating some D3D code I have with WPF via the new D3DImage
I'm currently trying to bind to certain items in a collection in wpf. This
I'm trying to write a simple WPF app that has two ellipses, joined by
I'm just finding my way with WPF/Datagrid/Linq and I'm trying to find out how

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.