Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Combobox not updating when collection is changed

I am new to WPF.

I am trying to bind collection of string to combobox.

public ObservableCollection<string> ListString {get; set;}

Binding and datacontext are set as follows

<Window 
        x:Class="Assignment2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:validators="clr-namespace:Assignment2"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Self}, Path=.}">
    <Grid>
        <ComboBox  Height="23" HorizontalAlignment="Left" Margin="109,103,0,0" Name="StringComboBox" VerticalAlignment="Top" Width="120" SelectionChanged="StringComboBox_SelectionChanged">
            <ComboBox.ItemsSource>
                <Binding Path="ListString" BindsDirectlyToSource="True" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"></Binding>
            </ComboBox.ItemsSource>
        </ComboBox>

I came to know that this is happening because collection is updating. If I write

public MainWindow()
        {

            InputString = "";
            ListString = new ObservableCollection<string>();
            ListString.Add("AAA");
            ListString.Add("BBB");
            ListString.Add("CCC");
          InitializeComponent();

        }

It does work but if I am move InitializeComponent() above at first line as follows, it doesn't work.

  public MainWindow()
            {
               InitializeComponent();
                InputString = "";
                ListString = new ObservableCollection<string>();
                ListString.Add("AAA");
                ListString.Add("BBB");
                ListString.Add("CCC");                
            }

What Should I do??

like image 778
Ganesh Satpute Avatar asked Oct 24 '13 12:10

Ganesh Satpute


1 Answers

what happens if you change your code to

<Window 
    x:Class="Assignment2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:validators="clr-namespace:Assignment2"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ComboBox  Height="23" HorizontalAlignment="Left" Margin="109,103,0,0" Name="StringComboBox" VerticalAlignment="Top" Width="120" SelectionChanged="StringComboBox_SelectionChanged"
               ItemsSource="{Binding ListString, Mode=OneWay}"/>

cs.

  public MainWindow()
        {
           InitializeComponent();
            InputString = "";
            ListString = new ObservableCollection<string>();
            ListString.Add("AAA");
            ListString.Add("BBB");
            ListString.Add("CCC"); 

           this.DataContext=this;    
      }           

btw: setting the ItemsSource with mode=twoway makes no sense to me. your combobox will never "create a new itemssource" for your viewmodel.

EDIT: i think your first solution works because of setting the DataContext in xaml. i assume that DataContext="{Binding RelativeSource={RelativeSource Self}, Path=.}" is execute when calling InitializeComponent(); and because your ListString property is just an autoproperty and not implement INotifyPropertyChanged - your mainwindowview does not get notified that your ctor creates a new ListString property.

  public ObservableCollection<string> ListString {get{return _list;}; set{_list=value; OnPropertyChanged("ListString");}}

should work with both of your approaches, but you have to implement INotifyPropertyChanged for your MainWindow class.

like image 60
blindmeis Avatar answered Oct 02 '22 15:10

blindmeis