獲取工具下載 - DevExpress WinForm v21.2
命令觸發器
Triggers允許您執行與命令關聯的其他查看操作,根據觸發Triggers的條件,共有三種觸發器類型。
- “Before”觸發器 - 允許您在目標命令執行之前執行操作。
C#
mvvmContext.ViewModelType = typeof(ViewModelWithSimpleCommand); var fluent = mvvmContext.OfType<ViewModelWithSimpleCommand>(); fluent.BindCommand(commandButton, x => x.DoSomething); fluent.WithCommand(x => x.DoSomething) .Before(() => XtraMessageBox.Show("The target command is about to be executed"));
VB.NET
mvvmContext.ViewModelType = GetType(ViewModelWithSimpleCommand) Dim fluent = mvvmContext.OfType(Of ViewModelWithSimpleCommand)() fluent.BindCommand(commandButton, Function(x) x.DoSomething) fluent.WithCommand(Sub(x) x.DoSomething) .Before(Function() XtraMessageBox.Show("The target command is about to be executed"))
- “After”觸發器 - 允許您在目標命令完成後執行操作。
C#
mvvmContext.ViewModelType = typeof(ViewModelWithSimpleCommand); var fluent = mvvmContext.OfType<ViewModelWithSimpleCommand>(); fluent.BindCommand(commandButton, x => x.DoSomething); fluent.WithCommand(x => x.DoSomething) .After(() => XtraMessageBox.Show("The target command has been executed"));
VB.NET
mvvmContext.ViewModelType = GetType(ViewModelWithSimpleCommand) Dim fluent = mvvmContext.OfType(Of ViewModelWithSimpleCommand)() fluent.BindCommand(commandButton, Function(x) x.DoSomething) fluent.WithCommand(Function(x) x.DoSomething).After(Function() XtraMessageBox.Show("The target command has been executed"))
- “CanExecute”條件觸發器 - 允許您在目標命令的 CanExecute 條件更改時執行操作。
C#
var fluent = mvvmContext.OfType<ViewModelWithSimpleCommandAndCanExecute>(); fluent.BindCommand(commandButton, x => x.DoSomething); // When the CanExecute condition changes, the message shows up fluent.WithCommand(x => x.DoSomething) .OnCanExecuteChanged(() => XtraMessageBox.Show("The CanExecute condition has changed"));
VB.NET
Dim fluent = mvvmContext.OfType(Of ViewModelWithSimpleCommandAndCanExecute)() fluent.BindCommand(commandButton, Function(x) x.DoSomething) ' When the CanExecute condition changes, the message shows up fluent.WithCommand(Function(x) x.DoSomething) .OnCanExecuteChanged(Function() XtraMessageBox.Show("The CanExecute condition has changed"))
請注意,觸發器爲綁定到目標命令的每個 UI 元素執行。 單擊任何按鈕時,下面的代碼示例會顯示一個消息框。
C#
mvvmContext1.OfType<BulkEditViewModel>() .WithCommand(vm => vm.RemoveFields()) .Bind(button1) .Bind(button2) .After(() => MessageBox.Show("Test"));
VB.NET
mvvmContext1.OfType(Of BulkEditViewModel)() .WithCommand(Function(vm) vm.RemoveFields()) .Bind(button1) .Bind(button2) .After(Function() MessageBox.Show("Test"))
非 POCO 命令
上面描述的 POCO 類命令允許您使用最直接和無故障的語法,DevExpress MVVM 框架還支持其他命令類型 - 以確保舊項目的輕鬆遷移。
DevExpress 委託命令對象
委託命令是 System.Windows.Input.ICommand 接口的實現。
運行demo: Simple Commands
C#
DelegateCommand command = new DelegateCommand(() => { XtraMessageBox.Show("Hello!"); }); commandButton.BindCommand(command);
VB.NET
Dim command As New DelegateCommand(Sub() XtraMessageBox.Show("Hello!")) commandButton.BindCommand(command)
運行demo:Commands with CanExecute Conditions
C#
Func<bool> canExecute = () => (2 + 2 == 4); DelegateCommand command = new DelegateCommand(() => { XtraMessageBox.Show("Hello!"); }, canExecute); commandButton.BindCommand(command);
VB.NET
Dim canExecute As Func(Of Boolean) = Function() (2 + 2 = 4) Dim command As New DelegateCommand(Sub() XtraMessageBox.Show("Hello!"), canExecute) commandButton.BindCommand(command)
運行demo:Commands with Parameters
C#
DelegateCommand<object> command = new DelegateCommand<object>((v) => { XtraMessageBox.Show(string.Format("The parameter is {0}.", v)); }); object parameter = 5; commandButton.BindCommand(command, () => parameter);
VB.NET
Dim command As New DelegateCommand(Of Object)(Sub(v) XtraMessageBox.Show(String.Format("The parameter is {0}.", v))) Dim parameter As Object = 5 commandButton.BindCommand(command, Function() parameter)
運行demo:Commands with Parameterized CanExecute Conditions
C#
Func<int, bool> canExecute = (p) => (2 + 2 == p); DelegateCommand<int> command = new DelegateCommand<int>((v) => { XtraMessageBox.Show(string.Format("The parameter is {0}.", v)); }, canExecute); int parameter = 4; commandButton.BindCommand(command, () => parameter);
VB.NET
Dim canExecute As Func(Of Integer, Boolean) = Function(p) (2 + 2 = p) Dim command As New DelegateCommand(Of Integer)(Sub(v) XtraMessageBox.Show(String.Format("The parameter is {0}.", v)), canExecute) Dim parameter As Integer = 4 commandButton.BindCommand(command, Function() parameter)
自定義命令類
這些是具有至少一個 Execute 方法的任何自定義類型的對象。 如果需要,您可以添加 CanExecute 方法和 CanExecuteChanged 事件。
運行demo:Simple Commands
C#
CommandObject command = new CommandObject(); commandButton.BindCommand(command); public class CommandObject { public void Execute(object parameter) { XtraMessageBox.Show("Hello!"); } }
VB.NET
Private command As New CommandObject() commandButton.BindCommand(command) Public Class CommandObject Public Sub Execute(ByVal parameter As Object) XtraMessageBox.Show("Hello!") End Sub End Class
運行demo:Commands with Parameters
C#
CommandObjectWithParameter command = new CommandObjectWithParameter(); int parameter = 4; commandButton.BindCommand(command, () => parameter); public class CommandObjectWithParameter { public void Execute(object parameter) { XtraMessageBox.Show(string.Format( "The parameter is {0}.", parameter)); } public bool CanExecute(object parameter) { return object.Equals(2 + 2, parameter); } }
VB.NET
Dim command As New CommandObjectWithParameter() Dim parameter As Integer = 4 commandButton.BindCommand(command, Sub() parameter) Public Class CommandObjectWithParameter Public Sub Execute(ByVal parameter As Object) XtraMessageBox.Show(String.Format("The parameter is {0}.", parameter)) End Sub Public Function CanExecute(ByVal parameter As Object) As Boolean Return Object.Equals(2 + 2, parameter) End Function End Class
DevExpress WinForm擁有180+組件和UI庫,能爲Windows Forms平臺創建具有影響力的業務解決方案。DevExpress WinForms能完美構建流暢、美觀且易於使用的應用程序,無論是Office風格的界面,還是分析處理大批量的業務數據,它都能輕鬆勝任!
DevExpress技術交流羣5:742234706 歡迎一起進羣討論