首先
在窗口中引入 MVVMcontext 组件
工具栏查找拖拽就行
设计界面 点MVVMcontext, 的 addViewodel
会自动生成ViewModel 的类和一些代码
``namespace DEVMVVMDemo
{
[POCOViewModel()]
public class Form1ViewModel
{
}
}`
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
public Form1()
{
InitializeComponent();
if (!mvvmContext1.IsDesignMode)
InitializeBindings();
}
private void Form1_Load(object sender, EventArgs e)
{
}
void InitializeBindings()
{
var fluent = mvvmContext1.OfType<Form1ViewModel>();
}
}
在DEV MVVM 中 每个view 都要有 一个 ViewModel
在ViewModel 类里定义属性
public class Form1ViewModel
{
public string MyProperty { get; set; }
}
}
在 主窗口绑定
void InitializeBindings()
{
var fluent = mvvmContext1.OfType<Form1ViewModel>();
fluent.SetBinding(this, view => view.Text, x => x.MyProperty);
}
最简单的数据字段绑定
如果要给一个控件的文本绑定值 最简单的绑定就已经完成了
给 label 绑定文本
void InitializeBindings()
{
var fluent = mvvmContext1.OfType<Form1ViewModel>();
// fluent.SetBinding(this, view => view.Text, x => x.MyProperty);
fluent.SetBinding(labelControl1, lab => lab.Text, x => x.MyProperty);
}
ViewModel 中定义要绑定的值
namespace DEVMVVMDemo
{
[POCOViewModel()]
public class Form1ViewModel
{
public string a = "ok";
public string MyProperty { get {
return a;
}
set {
} }
}
}