I'm stuck with this and even after reading a lot of topics I can find the answer.
Here my attempt to bind an observable Collection to a combobox in WPF using MVVM Pattern:
Scenario.cs
{
public class Scenario
{
public string name { get; set; }
public string codeClient { get; set; }
public string codeAppli { get; set; }
public string infoComplementaire { get; set; }
}
}
scenarioVM.cs
{
public ObservableCollection<Scenario> Scenarios { get; set; }
}
MainWindows.xaml
<ComboBox x:Name="cbScenario" ItemsSource="{Binding Scenarios}" DisplayMemberPath="{Binding Path=Name}" HorizontalAlignment="Left" Margin="407,8,0,0" VerticalAlignment="Top" Width="226" BorderBrush="#FF1585B5" Height="26"/>
Thanx for the help :)
If you set your DataContext
to this
, ofc you can't find Scenarios
if its part of your ScenarioVM
and not your Window. DataContext
is the root of any Binding.
For a start you could do this.
public ScenarioVM VM {get; private set;}
public MainWindow()
{
VM = new ScenarioVM();
InitializeComponent();
DataContext = VM;
this.Loaded += MetroWindow_Loaded;
VM.Scenarios.Add(new Scenario());
}
Now your window owns an instance of the ScenarioVM. Not necessarily a good design, but a start.
Also sooner than later you run into Trouble that your Scenario is not deriving from INotifyPropertyChanged
so you might want to address that as well.