Essayez de comprendre WPF. Voici mes classes de test :
public partial class MainWindow : Window, INotifyPropertyChanged
{
private ObservableCollection<string> _myList = new ObservableCollection<string>();
public ObservableCollection<string> MyList
{
get { return _myList; }
set
{
_myList = value;
RaisePropertyChanged("_myList");
}
}
public MainWindow()
{
InitializeComponent();
comboBox1.DataContext = _myList;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
MyList = AnotherClass.SomeMethod();
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
internal static class AnotherClass
{
public static ObservableCollection<string> SomeMethod()
{
return new ObservableCollection<string> {"this","is","test"};
}
}
Et voici XAML
<Grid>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="65,51,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" ItemsSource="{Binding}" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="310,51,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
Comment faire fonctionner ce code ? Je veux que les données de la ComboBox soient modifiées après avoir cliqué sur le bouton et que MyList soit mise à jour. PropertyChangedEventHandler est toujours nul.