我有一个 WPF 窗口,在那个窗口中我有一个网格。
我使用 MV-VM 模型,我想在代码中动态地将 TextBox 添加到网格中(在 viewmodel 中)
如何访问网格?
我有一个 WPF 窗口,在那个窗口中我有一个网格。
我使用 MV-VM 模型,我想在代码中动态地将 TextBox 添加到网格中(在 viewmodel 中)
如何访问网格?
使用监督控制器模式。
阅读:
此处显示了CaliburnMicro MVVM 框架的示例实现(对所有其他框架都一样 - 或者如果您自己执行 MVVM,您可以手动执行):
http://drc.ideablade.com/devforce-2012/bin/view/Documentation/cocktail-tutorial-talk-to-view
例子:
1)定义接口IView
,ViewModel
( VM
) 将在其中View
与所需的方法进行对话
public interface IView
{
void AddTextBoxToGrid();
}
2)从你的和实现方法继承代码View
IView
IView.AddTextboxToGrid()
public partial class View: IView
{
public void AddTextBoxToGrid()
{
// implement here your custom view logic using standard code behind;
}
}
3)将类型的属性添加IView
到您的VM
public class ViewModel
{
public IView View { get; set; }
}
4)将View
属性VM
View
设置为as的实例,IView
例如在后面的代码中:
DataContext.View = this as IView;
或在 Caliburn 中,您可以使用 IScreen.OnViewAttached 覆盖方法)
public partial class View: IView
{
public View()
{
// access you VM by the strategy of your framework or choice - this example is when you store your VM in View's DataContext
(DataContext as ViewModel).View = this as IView;
}
public void AddTextBoxToGrid()
{
// implement here your custom view logic using standard code behind;
}
}
5) In your VM
call IView.AddTextboxToGrid()
public class ViewModel
{
public IView View { get; set; }
public void AddTextBoxToGrid()
{
if (View == null) return;
View.AddTextBoxToGrid()
}
}
您应该将创建代码移至 View,并且 ViewModel 应该在应该调用它时通知视图。
您还可以在视图后面的代码中使用视图的 DataContext(即 ViewModel),并将文本框添加到那里的网格中。那会更有意义。
如果您在 XAML 文件中为网格命名,您将能够立即在后面的代码中访问网格。
If you are using Caliburn Micro, implement following step:
Make the ViewModel inherited from interface IViewAware
; you are going to implement two methods AttachView and GetView of this interface.
Define a variable with type of View to get the reference to the View
See detail below:
private SomeViewClass v;
public void AttachView(object view, object context = null)
{
v = view as BomView;
if (ViewAttached != null)
ViewAttached(this,
new ViewAttachedEventArgs() { Context = context, View = view });
}
public object GetView(object context = null)
{
return v;
}
Later on you can access a single element on the View through v such as v.txtName="John"; etc...