WPF MVVM模式开发实现简明教程 2 初识 INotifyPropertyChanged
WPF MVVM模式开发实现简明教程 3-1 BaseCommand
WPF MVVM模式开发实现简明教程 4 ViewModelBase
WPF MVVM模式开发简明实现教程 5 使用MultiValueConverter进行多参数事件绑定
WPF MVVM模式开发简明实现教程 7 DevExpress MVVM
可以看到比原生的用法简单很多,POCO View 结合 DXCommand很方便
自己其实也可以实现
ViewModelBase
View
<Button Command="{Binding LoginCommand}">Login</Button> <TextBlock Text="{Binding Status}" />
ViewModel
public string Status { get { return GetValue<string>(); } private set { SetValue(value); } } [Command] public void Login() { Status = "User: " + UserName; } public bool CanLogin() { return !string.IsNullOrEmpty(UserName); }
POCO View
View
<Button Command="{Binding LoginCommand}">Login</Button> <TextBlock Text="{Binding Status}" />
ViewModel
public virtual string Status { get; protected set; } // LoginCommand will be created for the Login and CanLogin methods: public void Login() { Status = "User: " + UserName; } public bool CanLogin() { return !string.IsNullOrEmpty(UserName); }
DelegateCommand
view
<dxlc:LayoutControl Orientation="Vertical" VerticalAlignment="Top"> <dxlc:LayoutGroup Header="Command without parameter" Orientation="Vertical" View="GroupBox"> <CheckBox IsChecked="{Binding CanExecuteSaveCommand}" Content="Can Save"/> <Button Command="{Binding SaveCommand}">Save</Button> </dxlc:LayoutGroup> <dxlc:LayoutGroup Header="Command with parameter" Orientation="Vertical" View="GroupBox"> <dxe:TextEdit Text="{Binding FileName, UpdateSourceTrigger=PropertyChanged}" NullText="Enter file name"/> <Button Command="{Binding OpenCommand}" CommandParameter="{Binding FileName}">Open</Button> </dxlc:LayoutGroup> </dxlc:LayoutControl>
ViewModel
public ICommand SaveCommand { get; private set; } public ICommand OpenCommand { get; private set; } public DelegateCommandsViewModel() { CanExecuteSaveCommand = true; SaveCommand = new DelegateCommand( () => MessageBox.Show("Action: Save"), () => CanExecuteSaveCommand); OpenCommand = new DelegateCommand<string>( fileName => MessageBox.Show(string.Format("Action: Open {0}", FileName)), fileName => !string.IsNullOrEmpty(fileName)); } public bool CanExecuteSaveCommand { get { return GetValue<bool>(); } set { SetValue(value); } } public string FileName { get { return GetValue<string>(); } set { SetValue(value); } }
DXCommand
View
<dxg:GridControl MouseDoubleClick="{DXEvent 'GridControl_MouseDoubleClick(@sender, @args)'}" >
ViewModel
public void GridControl_MouseDoubleClick(object sender, MouseButtonEventArgs e) { var row = (sender as GridControl).CurrentItem; if (!(row is CreateIncidentInfomationModel createIncidentInfoModel)) { return; } }