1.新建一个WinForm 项目,省略图片
2.安装 ReactiveUI
3.新建Model,继承Reactiveobject
namespace ElectronBeamSystem.Model
{
public class MenuButton: ReactiveObject
{
private string _menuText;
private Color _menuColor;
private Color _menuFontColor;
public MenuButton()
{
_menuText = "MVVM控件";
_menuColor = Color.Blue;
_menuFontColor = Color.White;
}
public string MenuText {
get=>_menuText;
set=>this.RaiseAndSetIfChanged(ref _menuText,value);
}
public Color MenuColor
{
get=>_menuColor;
set => this.RaiseAndSetIfChanged(ref _menuColor, value);
}
public Color MenuFontColor
{
get=> _menuFontColor;
set => this.RaiseAndSetIfChanged(ref _menuFontColor, value);
}
}
}
3.新建ModelView
namespace ElectronBeamSystem.ViewMode
{
public class MenuButtonModelView: ReactiveObject
{
private MenuButton _menuButton;
private List<MenuButton> menuButtons ;
public ReactiveCommand<Unit, Unit> ExampleCommand { get; } //定义命令
public ReactiveCommand<Unit, Unit> ExampleCommandText { get; }
public ReactiveCommand<Unit, Unit> ExampleCommandAdd { get; }
public MenuButton _MenuButton {
get=>_menuButton; set=>this.RaiseAndSetIfChanged(ref _menuButton,value);
}
public List<MenuButton> _MenuButtons {
get=> menuButtons; set=>this.RaiseAndSetIfChanged(ref menuButtons, value);
}
public MenuButtonModelView()
{
_menuButton = new MenuButton();
_MenuButtons= new List<MenuButton>();
_MenuButtons.Add(new MenuButton());
ExampleCommand = ReactiveCommand.Create(PerformAction); //绑定事件
ExampleCommandText = ReactiveCommand.Create(PerformActionText);
ExampleCommandAdd = ReactiveCommand.Create(PerformActionxx);
}
private void PerformActionText()
{
}
private void PerformActionxx()
{
}
private void PerformAction()
{
_menuButton.MenuColor = Color.Red;
Console.WriteLine(_menuButton.MenuText);
Console.WriteLine(_menuButton.MenuFontColor);
Console.WriteLine(_menuButton.MenuColor);
foreach (var item in _MenuButtons)
{
Console.WriteLine(item.MenuText);
}
}
}
}
4.Form界面进行数据双向绑定
namespace ElectronBeamSystem
{
public partial class Form1 : Form, IViewFor<MenuButtonModelView>
{
public Form1()
{
InitializeComponent();
this.WhenActivated(x =>
{
x(this.Bind(ViewModel, vm => vm._MenuButton.MenuText, v => v.button1.Text));
x(this.Bind(ViewModel, vm => vm._MenuButton.MenuColor, v => v.button1.BackColor));
x(this.Bind(ViewModel, vm => vm._MenuButton.MenuFontColor, v => v.button1.ForeColor));
x(this.Bind(ViewModel, vm => vm._MenuButton.MenuText, v => v.textBox1.Text));
x(this.BindCommand(ViewModel, vm => vm.ExampleCommand, v => v.button1));
x(this.BindCommand(ViewModel, vm => vm.ExampleCommandText, v => v.textBox1, "DoubleClick"));
x(this.Bind(ViewModel, vm => vm._MenuButtons, v => v.dataGridView1.DataSource));
x(this.BindCommand(ViewModel, vm => vm.ExampleCommandAdd, v => v.button2));
});
ViewModel = new MenuButtonModelView();
}
public MenuButtonModelView ViewModel { get; set; }
object IViewFor.ViewModel { get => ViewModel; set => ViewModel = (MenuButtonModelView)value; }
}
}
最终显示效果:
总结:第一次写文章,做个记录,上面的DataGrivdView 数据无法进行实时更新,有那位大佬知道的麻烦指导一下.绑定命令时传递参数在官网也没有找到相关实列. 官网: ReactiveUIUsing ReactiveUI for WinForms MVVM Design - CodeProject
ps: DataGrid View 数据更新声明集合时采用BindingList 就可以及时更新