ThaMe90
ThaMe90

Reputation: 4296

Simple WPF data binding

I want to separate my user interface from my code, so I (obviously) landed at bindings. As a test, I've written the following XAML:

<Window x:Class="BindingTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="Auto" Width="200">
    <StackPanel>
        <TextBox Text="{Binding Item}"/>
        <Button Content="Add" Click="AddNew"/>
        <ListBox Height="100" ItemsSource="{Binding Items}"/>
    </StackPanel>
</Window>

The C# looks like this:

namespace BindingTest
{
    public partial class MainWindow : Window
    {
        public string Item { get; set; }
        public ObservableCollection<string> Items { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            Items = new ObservableCollection<string>();
        }

        private void AddNew(object sender, RoutedEventArgs e)
        {
            Items.Add(Item);
        }
    }
}

What I want to happen is that the text entered into the textbox is added to the listbox's itemssource. However, this doesn't happen...

Upvotes: 0

Views: 807

Answers (5)

user1101511
user1101511

Reputation:

Try this

hope this will work. But this is not hte right approach. You need to set the DataContext to the Object whose properties u guna use for binding. you must follow MVVM Architecture.

Upvotes: 0

Martin Liversage
Martin Liversage

Reputation: 106926

Data binding is performed against the current data context. However, you have not set the data context for your window. Often you will set the data context to a view model but in your case you simply want to use the window class for that.

You should add the following line to the constructor:

DataContext = this;

Upvotes: 1

Jefim
Jefim

Reputation: 3077

Two things:

  1. You should set the correct data context for your window. Otherwise the binding will not find your properties.
  2. You should initialize your Items collection before the InitializeComponent() call as inside it the ListBox tries to evaluate the expression and get NULL as the binding souce. And since you are not implementing INotifyPropertyChanged and the property is not a DependencyProperty the ListBox will never reevaluate the binding thus it will never get the instance of your Items collection.

So, the code should be as follows:

    public MainWindow()
    {
        Items = new ObservableCollection<string>();
        DataContext = this;
        InitializeComponent(); 
    }

Upvotes: 0

Russell Troywest
Russell Troywest

Reputation: 8776

Change your code to this:

    public partial class MainWindow : Window
    {
        public string Item { get; set; } 
        public ObservableCollection<string> Items { get; set; } 
        public MainWindow()
        {
            InitializeComponent(); 
            Items = new ObservableCollection<string>();
            DataContext = this;
        }

        private void AddNew(object sender, RoutedEventArgs e)
        {
            Items.Add(Item);
        }
    }
}

You do need to set your DataContext - works for me.

Upvotes: 0

Dror
Dror

Reputation: 2588

Two things you need two do -

  1. Set - DataContext = this; in your constructor.
  2. You'd be better off if you would change your properties to dependency properties instead. You could do that easily with the "propdp" snippet in visual studio.

Upvotes: 2

Related Questions