Thursday, 5 September 2013

How to bind Variables of View to ViewModel in WPF MVVM?

How to bind Variables of View to ViewModel in WPF MVVM?

I have a created a window ( WPF and MVVM ) - say PrintWidow ( so I have
PrintWindow.xaml , PrintWindow.xaml.cs , PrintWindowViewModel.cs-
viewmodel)
Now I am going to use(call) this PrintWindow obj from some other class on
button click or on some command trigger , I want to set Document Source
for this PrintWindow(following MVVM).
How would I do this ? I created a PrintDocument object in
PrintWindow.xaml.cs and tried to bind it as follows : (obviously just a
blank try - as I cannot do this declaration in XAML)
private PrintDocument printDocuementView;
public PrintDocument PrintDocuement
{
get { return printDocuementView; }
set { printDocuementView = value; }
}
//constructor
public PrintWindow()
{
InitializeComponent();
this.DataContext = new PrintViewModel();
Binding b = new Binding();
b.Source = printDocuementView;
b.Path = new PropertyPath("PrintDocumentCommand"); //
"PrintDocumentCommand" is defined in View Model class and is
responsible to set the `PrintDocument` object there.
}
This code (obviously) doesn't work. How should I go about it.
I have googled for answers - but couldn't relate any to my problem.
I am a Freshman for WPF and a Rookie for MVVM.
Any help will be much appreciated.
Thanks in advance.

No comments:

Post a Comment