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 ItemsSource with mode = twoway doesn't make any sense to me. your combobox will never βcreate a new data sourceβ for your view model.
EDIT: I think your first solution works due to setting DataContext in xaml. I assume that DataContext = "{Binding RelativeSource = {RelativeSource Self}, Path =.}" Is executed when InitializeComponent () is called; and because your ListString property is just an auto-processor and does not implement INotifyPropertyChanged - your mainwindowview does not receive notification that your ctor is creating 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 must implement INotifyPropertyChanged for your MainWindow class.
source share