彩票走势图

logo DevExpress WinForm中文手册

操作


立即下载DevExpress WinForms

操作在不修改对象的情况下为对象添加额外的功能。例如,关闭按钮关闭选项卡或表单,并显示一个确认对话框,您可以在MVVM应用程序中使用操作来实现这一点。

  • 确认操作
  • 事件到命令的操作
  • 键命令操作
  • 自定义操作

确认行为

提示:

MVVM 最佳实践演示下面的文本在DevExpressMVVM 最佳实践演示中有一个相关示例。

Group:API 代码示例

Module:操作

Example:简单操作

23.1 Demo Center:启动演示

一个简单的确认操作只能附加到一个可取消的事件(例如,表单关闭或编辑值更改)。要实现自定义操作,定义一个从ConfirmationBehavior类派生的类,它包含两个虚拟字符串属性,用于确认消息框的标题和文本。覆盖这些属性来分配文本字符串,构造函数应该继承基类构造函数,其参数等于触发此行为的事件的名称。

C#:

public class FormCloseBehavior : ConfirmationBehavior<FormClosingEventArgs> {
public FormCloseBehavior() : base("FormClosing") { }

protected override string GetConfirmationCaption() {
return "Confirm exit";
}

protected override string GetConfirmationText() {
return "Do you really want to exit the application?";
}
}

VB.NET:

Public Class FormCloseBehavior
Inherits ConfirmationBehavior(Of FormClosingEventArgs)

Public Sub New()
MyBase.New("FormClosing")
End Sub

Protected Overrides Function GetConfirmationCaption() As String
Return "Confirm exit"
End Function

Protected Overrides Function GetConfirmationText() As String
Return "Do you want to exit the application?"
End Function
End Class

ConfirmationBehavior类的最后一个虚拟属性是布尔类型的Confirm属性,您还可以覆盖它来指定相关事件的取消条件。

C#:

//Inverted confirmation logic
protected override bool Confirm() {
if(MessageBox.Show(GetConfirmationText(), GetConfirmationCaption(), MessageBoxButtons.YesNo) == DialogResult.Yes) return false;
else return true;
}

VB.NET:

'Inverted confirmation logic
Protected Overrides Function Confirm() As Boolean
If MessageBox.Show(GetConfirmationText(), GetConfirmationCaption(), MessageBoxButtons.YesNo) = DialogResult.Yes Then
Return False
Else
Return True
End If
End Function

使用MvvmContext组件的API将此操作附加到目标UI元素。

C#:

//View
mvvmContext.AttachBehavior<FormCloseBehavior>(this);

VB.NET:

'View
mvvmContext.AttachBehavior(Of FormCloseBehavior)(Me)

您可以通过将操作声明从单独的类移至通用类来简化此过程。

C#:

mvvmContext1.AttachBehavior<ConfirmationBehavior<ChangingEventArgs>>(checkEdit1, behavior => {
behavior.Caption = "CheckEdit State Changing";
behavior.Text = "This checkEdit's checked-state is about to be changed. Are you sure?";
behavior.Buttons = ConfirmationButtons.YesNo;
behavior.ShowQuestionIcon = true;
}, "EditValueChanging");

VB.NET:

mvvmContext1.AttachBehavior(Of ConfirmationBehavior(Of ChangingEventArgs))(checkEdit1, Sub(behavior)
behavior.Caption = "CheckEdit State Changing"
behavior.Text = "This checkEdit's checked-state is about to be changed. Are you sure?"
behavior.Buttons = ConfirmationButtons.YesNo
behavior.ShowQuestionIcon = True
End Sub, "EditValueChanging")

还支持 Fluent API。

C#:

mvvmContext1.WithEvent<ChangingEventArgs>(checkEdit1, "EditValueChanging").Confirmation(behavior => {
behavior.Caption = "CheckEdit State changing";
behavior.Text = "This checkEdit's checked-state is about to be changed. Are you sure?";
});

VB.NET:

mvvmContext1.WithEvent(Of ChangingEventArgs)(checkEdit1, "EditValueChanging").Confirmation(Sub(behavior)
behavior.Caption = "CheckEdit State changing"
behavior.Text = "This checkEdit's checked-state is about to be changed. Are you sure?"
End Sub)

事件到命令的操作

提示:

下面的文本在DevExpress的MVVM最佳实践演示中有一个相关的例子。

Group: API 代码示例

Module:操作

Example:事件到命令的操作

23.1 Demo Center:启动演示

如果确认操作需要接收CancelEventArgs类型参数的事件,则事件到命令操作适合所有剩余的事件,此操作将命令绑定到目标UI元素,当该元素触发所需事件时,命令将执行。这可以用于:

  • 第三方UI元素没有实现ISupportCommandBinding接口,因此不能使用(mvvmContext.BindCommand方法)进行绑定。
  • 需要额外功能的DevExpress控件。例如,如果您需要 SimpleButton在MouseHover事件上执行某些操作。

事件到命令操作的实现与确认操作类似:您需要定义一个派生自 DevExpress EventToCommandBehavior类的单独类,在类构造函数中,指定目标事件名称以及应对此事件执行的命令。

C#:

public class ClickToSayHello : DevExpress.Utils.MVVM.EventToCommandBehavior<ViewModel, EventArgs> {
public ClickToSayHello()
: base("Click", x => x.SayHello()) {
}
}

VB.NET:

Public Class ClickToSayHello
Inherits DevExpress.Utils.MVVM.EventToCommandBehavior(Of ViewModel, EventArgs)
Public Sub New()
MyBase.New("Click", Function(x) x.SayHello())
End Sub
End Class

将事件附加到命令操作与确认操作相同。

C#:

mvvmContext.AttachBehavior<ClickToSayHello>(thirdPartyButton);

VB.NET:

mvvmContext.AttachBehavior(Of ClickToSayHello)(thirdPartyButton)

Fluent API 允许您实现事件到命令的操作,而无需创建单独的类。

C#:

mvvmContext.WithEvent<ViewModel, EventArgs>(thirdPartyButton, "Click").EventToCommand(x => x.SayHello());

VB.NET:

mvvmContext.WithEvent(Of ViewModel, EventArgs)(thirdPartyButton, "Click").EventToCommand(Function(x) x.SayHello())

键到命令和键到命令操作

提示:

下面的文本在DevExpress的MVVM最佳实践演示中有一个相关的例子。

Group: API 代码示例

Module:操作

Example:按键到命令和按键到命令操作

23.1 Demo Center:启动演示

这些行为允许您在最终用户按下特定键盘键时执行命令。

绑定单个键盘快捷键

下面的示例ViewModel定义了显示基于服务的通知的“OnAKey”和“OnAltKey”命令。

C#:

public class KeyAwareViewModel {
protected IMessageBoxService MessageBoxService {
get { return this.GetService<IMessageBoxService>(); }
}
public void OnAKey() {
MessageBoxService.ShowMessage("Key Command: A");
}
public void OnAltAKey() {
MessageBoxService.ShowMessage("Key Command: Alt+A");
}
}

VB.NET:

Public Class KeyAwareViewModel
Protected ReadOnly Property MessageBoxService() As IMessageBoxService
Get
Return Me.GetService(Of IMessageBoxService)()
End Get
End Property
Public Sub OnAKey()
MessageBoxService.ShowMessage("Key Command: A")
End Sub
Public Sub OnAltAKey()
MessageBoxService.ShowMessage("Key Command: Alt+A")
End Sub
End Class

使用以下MvvmContext组件的流畅API方法将这些命令绑定到相关的键:

  • WithKey:第一个方法参数是一个UI元素,当最终用户按下某些键时,必须聚焦该元素,第二个参数是一个组合键。
  • KeyToCommand:表示要执行的命令。

下面的代码分别将“OnAKey”和“OnAltKey”命令绑定到“A”和“Alt+A”键,只有当备忘录编辑控件当前有焦点时,按下这两个组合键才能触发相关命令。

C#:

mvvmContext.ViewModelType = typeof(KeyAwareViewModel);
// Binding the "A" key
mvvmContext.OfType<KeyAwareViewModel>()
.WithKey(memo, Keys.A)
.KeyToCommand(x => x.OnAKey());
// Binding the "Alt+A" shortcut
mvvmContext.OfType<KeyAwareViewModel>()
.WithKey(memo, Keys.A | Keys.Alt)
.KeyToCommand(x => x.OnAltAKey());

VB.NET:

mvvmContext.ViewModelType = GetType(KeyAwareViewModel)
' Binding the "A" key
mvvmContext.OfType(Of KeyAwareViewModel)().WithKey(memo, Keys.A).KeyToCommand(Function(x) x.OnAKey())
' Binding the "Alt+A" shortcut
mvvmContext.OfType(Of KeyAwareViewModel)().WithKey(memo, Keys.A Or Keys.Alt).KeyToCommand(Function(x) x.OnAltAKey())
绑定多个键到同一个命令

您希望将多个键绑定到同一个命令并将这些键作为参数传递,请使用WithKeys和KeysToCommands方法。下面是示例KeyAwareViewModel视图模型,包含参数化的“OnKey”和“OnKeyArgs”命令,这些命令通知用户按下了哪些键。

C#:

public class KeyAwareViewModel {
protected IMessageBoxService MessageBoxService {
get { return this.GetService<IMessageBoxService>(); }
public void OnKey(Keys keys) {
MessageBoxService.ShowMessage("Key Command:" + keys.ToString());
}
public void OnKeyArgs(KeyEventArgs args) {
string message = string.Join(", ",
"KeyValue: " + args.KeyValue.ToString(),
"KeyData: " + args.KeyData.ToString(),
"KeyCode: " + args.KeyCode.ToString(),
"Modifiers: " + args.Modifiers.ToString());
MessageBoxService.ShowMessage("Args = {" + message + "}");
}
}

VB.NET:

Public Class KeyAwareViewModel
Protected ReadOnly Property MessageBoxService() As IMessageBoxService
Get
Return Me.GetService(Of IMessageBoxService)()
End Get
public void OnKey(Keys keys)
MessageBoxService.ShowMessage("Key Command:" & keys.ToString())
public void OnKeyArgs(KeyEventArgs args)
Dim message As String = String.Join(", ", "KeyValue: " & args.KeyValue.ToString(), "KeyData: " & args.KeyData.ToString(), "KeyCode: " & args.KeyCode.ToString(), "Modifiers: " & args.Modifiers.ToString())
MessageBoxService.ShowMessage("Args = {" & message & "}")
End Property

下面的代码将这些命令同时绑定到多个热键。

C#:

mvvmContext.ViewModelType = typeof(KeyAwareViewModel);
// Binding to the OnKey command
mvvmContext.OfType<KeyAwareViewModel>()
.WithKeys(memo, new Keys[] { Keys.A, Keys.B, Keys.C })
.KeysToCommand(x => x.OnKey(Keys.None), args => args.KeyCode);
// Binding to the OnKeyArgs command
mvvmContext.OfType<KeyAwareViewModel>()
.WithKeys(memo, new Keys[] { Keys.Shift | Keys.A, Keys.Shift | Keys.B, Keys.Shift | Keys.C})
.KeysToCommand(x => x.OnKeyArgs((KeyEventArgs)null), args => (KeyEventArgs)args);

VB.NET:

mvvmContext.ViewModelType = GetType(KeyAwareViewModel)
' Binding to the OnKey command
mvvmContext.OfType(Of KeyAwareViewModel)().WithKeys(memo, New Keys() { Keys.A, Keys.B, Keys.C }).KeysToCommand(Function(x) x.OnKey(Keys.None), Function(args) args.KeyCode)
' Binding to the OnKeyArgs command
mvvmContext.OfType(Of KeyAwareViewModel)().WithKeys(memo, New Keys() { Keys.Shift Or Keys.A, Keys.Shift Or Keys.B, Keys.Shift Or Keys.C}).KeysToCommand(Function(x) x.OnKeyArgs(DirectCast(Nothing, KeyEventArgs)), Function(args) CType(args, KeyEventArgs))

自定义操作

如果没有现成的DevExpress操作满足您的需求,您可以实现自定义操作。例如,默认情况下, Ctrl+C键盘组合会复制整个GridView行,如果您只需要复制所选行单元格的值,可以附加自己的操作。

C#:

public class ControlCBehavior : EventTriggerBase<System.Windows.Forms.KeyEventArgs> {
public ControlCBehavior()
: base("KeyDown") {
}
protected override void OnEvent() {
if (Args.Control && Args.KeyCode == System.Windows.Forms.Keys.C) {
var cbService = this.GetService<Services.IClipboardService>();
GridView View = Source as GridView;
if (View.GetRowCellValue(View.FocusedRowHandle, View.FocusedColumn) != null && View.GetRowCellValue(View.FocusedRowHandle, View.FocusedColumn).ToString() != String.Empty)
cbService.SetText(View.GetRowCellValue(View.FocusedRowHandle, View.FocusedColumn).ToString());
else
XtraMessageBox.Show("The value in the selected cell is null or empty!");
Args.Handled = true;
}
}
}

VB.NET:

Public Class ControlCBehavior
Inherits EventTriggerBase(Of System.Windows.Forms.KeyEventArgs)

Public Sub New()
MyBase.New("KeyDown")
End Sub
Protected Overrides Sub OnEvent()
If Args.Control AndAlso Args.KeyCode = System.Windows.Forms.Keys.C Then
Dim cbService = Me.GetService(Of Services.IClipboardService)()
Dim View As GridView = TryCast(Source, GridView)
If View.GetRowCellValue(View.FocusedRowHandle, View.FocusedColumn) IsNot Nothing AndAlso View.GetRowCellValue(View.FocusedRowHandle, View.FocusedColumn).ToString() <> String.Empty Then
cbService.SetText(View.GetRowCellValue(View.FocusedRowHandle, View.FocusedColumn).ToString())
Else
XtraMessageBox.Show("The value in the selected cell is null or empty!")
End If
Args.Handled = True
End If
End Sub
End Class

这个操作使用自定义的IClipboardService服务将文本复制到剪贴板。

C#:

public class ClipboardService : IClipboardService {
public void SetText(string text) {
Clipboard.SetText(text);
}
}

public interface IClipboardService {
void SetText(string text);
}

VB.NET:

Public Class ClipboardService
Implements IClipboardService

Public Sub SetText(ByVal text As String) Implements IClipboardService.SetText
Clipboard.SetText(text)
End Sub
End Class

Public Interface IClipboardService
Sub SetText(ByVal text As String)
End Interface

自定义操作准备好之后,注册服务并调用AttachBehavior方法将行为附加到网格视图。

C#:

mvvmContext1.RegisterService(new Services.ClipboardService());
mvvmContext1.AttachBehavior<Behaviors.ControlCBehavior>(gridView1);

VB.NET:

mvvmContext1.RegisterService(New Services.ClipboardService())
mvvmContext1.AttachBehavior(Of Behaviors.ControlCBehavior)(gridView1)

自定义操作现在抑制默认的快捷方式处理程序,并且只复制选定的单元格值。

扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP