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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:36:51+00:00 2026-05-25T17:36:51+00:00

I can get this working with an XmlDataSource but not with my own classes.

  • 0

I can get this working with an XmlDataSource but not with my own classes. All I want to do is bind the listbox to my collection instance and then link the textbox to the listbox so I can edit the person’s name (two-way). I’ve deliberately kept this as simple as possible in the hope that somebody can fill in the blanks.

XAML:

<Window x:Class="WpfListTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfListTest"
    Title="Window1" Height="300" Width="600">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="160"/>
            <ColumnDefinition Width="3"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <DockPanel Grid.Column="0">
            <ListBox />
        </DockPanel>
        <DockPanel Grid.Column="2">
            <StackPanel>
                <Label>Name</Label>
                <TextBox />
            </StackPanel>
        </DockPanel>
    </Grid>
</Window>

C# code behind:

namespace WpfListTest
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public People MyPeeps = new People();

        public Window1()
        {
            InitializeComponent();

            MyPeeps.Add(new Person("Fred"));
            MyPeeps.Add(new Person("Jack"));
            MyPeeps.Add(new Person("Jill"));
        }
    }

    public class Person
    {
        public string Name { get; set; }

        public Person(string newName)
        {
            Name = newName;
        }
    }

    public class People : List<Person>
    {
    }
}

All the examples on the web seem to have what is effectively a static class returning code-defined data (like return new Person(“blah blah”)) rather than my own instance of a collection – in this case MyPeeps. Or maybe I’m not uttering the right search incantation.

One day I might make a sudden breakthrough of understanding this binding stuff but at the moment it’s baffling me. Any help appreciated.

  • 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-25T17:36:52+00:00Added an answer on May 25, 2026 at 5:36 pm

    The correct way would be to use the MVVM pattern and create a ViewModel like so:

    public class MainWindowViewModel : INotifyPropertyChanged
    {
        private People _myPeeps;
        private Person _selectedPerson;
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public People MyPeeps
        {
            get { return _myPeeps; }
            set
            {
                if (_myPeeps == value)
                {
                    return;
                }
                _myPeeps = value;
                RaisePropertyChanged("MyPeeps");
            }
        }
    
        public Person SelectedPerson
        {
            get { return _selectedPerson; }
            set
            {
                if (_selectedPerson == value)
                {
                    return;
                }
                _selectedPerson = value;
                RaisePropertyChanged("SelectedPerson");
            }
        }
    
        private void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    

    Initialize it in your View’s code behind like so:

    public partial class MainWindow : Window
    {
        private readonly MainWindowViewModel _viewModel;
    
        public MainWindow()
        {
            _viewModel = new MainWindowViewModel();
            _viewModel.MyPeeps = new People();
            _viewModel.MyPeeps.Add(new Person("Fred"));
            _viewModel.MyPeeps.Add(new Person("Jack"));
            _viewModel.MyPeeps.Add(new Person("Jill"));
            DataContext = _viewModel;
    
            InitializeComponent();
        }
    }
    

    And bind the data like so:

    <Window x:Class="WpfApplication3.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">
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="160" />
          <ColumnDefinition Width="3" />
          <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>
        <DockPanel Grid.Column="0">
          <ListBox SelectedItem="{Binding SelectedPerson}"
                   DisplayMemberPath="Name"
                   ItemsSource="{Binding MyPeeps}" />
        </DockPanel>
        <DockPanel Grid.Column="2">
          <StackPanel>
            <Label>Name</Label>
            <TextBox Text="{Binding SelectedPerson.Name}" />
          </StackPanel>
        </DockPanel>
      </Grid>
    </Window>
    

    The binding will work like this:

    The DataContext of the window itself is set to the ViewModel instance. Because the ListBox and the TextBox don’t specify any DataContext, they inherit it from the Window. The bindings on an object always work relative to the DataContext if nothing else is being specified. That means that the TextBox binding looks for a property SelectedPerson in its DataContext (i.e., in the MainWindowViewModel) and for a Property Name in that SelectedPerson.

    The basic mechanics of this sample are as follows:
    The SelectedPerson property on the ViewModel is always synchronized with the SelectedItem of the ListBox and the Text property of the TextBox is always synchronized with the Name property of the SelectedPerson.

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

Sidebar

Related Questions

Sorry if this is obvious but have looked around and can't get this working:
I've been working all day and I somehow can't get this probably easy task
I read this post but I can't get it working: Change Background Color... I
This should be totally simple but I can't get it working no matter what
Any way I can get this working? I have a block of code: <p><a
I can't get this working for the life of me. Here is a snippet
Even using the default test code on my app I can't get this working.
I can't seem to get this working, what should happen is when the user
I can't get my head around why this isn't working.. I have a relatively
I have this simple example I can't seems to get working : MERGE INTO

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.