Reputation: 533
I need to initialize object in xaml.cs code behind and then use that object in my binded viewmodel. But when I do that the object properly initialize, but viewmodel acts like it's still null.
MainWindow.xaml
<oxys:PlotView x:Name="dataPlot" Model="{Binding DataPlotModel}" Margin="10,10,185,39"/>
MainWindow.xaml.cs
MainWindowViewModel viewModel;
public MainWindow()
{
viewModel = new MainWindowViewModel();
DataContext = viewModel;
InitializeComponent();
PlotModel DataPlotModel = new PlotModel();
dataPlot.Model = DataPlotModel;
}
MainWindowViewModel.cs
public PlotModel DataPlotModel { get; set; }
And the DataPlotModel
in viewmodel is always null unless I initialize it strictly in viewmodel.
Upvotes: 0
Views: 1585
Reputation: 169200
You need to set the DataPlotModel
property of the view model somewhere:
MainWindowViewModel viewModel;
public MainWindow()
{
viewModel = new MainWindowViewModel();
DataContext = viewModel;
InitializeComponent();
viewModel.DataPlotModel = new PlotModel(); //<-- Set the view model property
}
You should set the view model property rather than setting the property of control directly as this will break the binding.
Upvotes: 3