[英]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) 我创建了一个窗口(WPF和MVVM)-说PrintWidow(所以我有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). 现在,我将在单击按钮或通过某些命令触发器使用其他类中的(调用)此PrintWindow
obj时,我想为此PrintWindow设置Document Source(在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) 我在PrintWindow.xaml.cs中创建了一个PrintDocument
对象,并尝试按以下方式绑定它:(显然只是一个空的尝试-因为我不能在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. 我应该怎么做。 Summary : I want to open PrintWindow
from another window and eventually set some Property of PrintWindow
from code behind of the 'other widow' object.The query is - where should this property go? 简介:我想从另一个窗口打开PrintWindow
,并最终从``其他寡妇''对象后面的代码中设置PrintWindow
某些属性。查询是-该属性应该放在哪里? View ? 查看? ViewModel? ViewModel? ?? ?? puzzzeled 令人费解
I have googled for answers - but couldn't relate any to my problem. 我已经用Google搜索了答案-但无法与我的问题有关。
I am a Freshman for WPF
and a Rookie for MVVM
. 我是WPF
的新生,也是MVVM
的新秀。
Since your PrintDocumentCommand
is in your PrintViewModel
but you're setting the Source of this Binding to an instance of the PrintDocument
-Class, it can't be found, because the Binding is looking for the PrintDocumentCommand
in PrintDocument
-Class. 由于您的PrintDocumentCommand
是在你的PrintViewModel
但你设定的这种结合的实例源PrintDocument
级轿车,它不能被发现,因为绑定正在寻找PrintDocumentCommand
中PrintDocument
-Class。
If you want to open the PrintWindow from another Window, place the PrintDocument
-Property and the PrintDocumentCommand
in the ViewModel of the other Window. 如果要从另一个窗口打开PrintWindow,请将PrintDocument
-Property和PrintDocumentCommand
放在另一个窗口的ViewModel中。 Now your function that is executed through the PrintDocumentCommand
could look like: 现在,通过PrintDocumentCommand
执行的函数可能类似于:
private void Print()
{
PrintWindow pw = new PrintWindow(PrintDocument);
pw.ShowDialog();
}
The constructor of your PrintView could be like: 您的PrintView的构造函数可能类似于:
public PrintWindow(PrintDocument pd)
{
InitializeComponents();
this.DataContext = new PrintViewModel(pd);
}
and now you can access the PrintDocument in your PrintViewModel. 现在您可以在PrintViewModel中访问PrintDocument。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.