商信互联
本文介绍如何将自定义控件添加到详细信息视图。当您需要在自定义视图中的特定编辑器附近放置自定义控件并且“动作容器视图项”方法不合适时,请使用此方法。
DevExpress代码示例数据库(http://www.devexpress.com/example=T137193)中提供了完整的示例项目。
调用与平台有关的MySolution.Module.Win,MySolution.Module.Web或MySolution.Module.Mobile项目的模型编辑器,然后找到所需的“视图” |“视图”。DetailView节点。使用为Items子节点调用的上下文菜单添加IModelControlDetailItem节点。
聚焦意见| DetailView | 布局节点。右键单击空白处以调用布局自定义对话框,然后将新创建的控件放在所需的位置。有关如何更改布局的详细信息,请参阅“查看项目布局自定义”主题。
将新的View Controller添加到WinForms,ASP.NET或Mobile模块中,以处理在Application Model中添加的按钮的Click事件。订阅ViewItem.ControlCreated事件,并将自动生成的代码替换为以下内容。
WinForms
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Layout;
using DevExpress.ExpressApp.Win.Core;
using System.Windows.Forms;
// ...
public class ControlViewItemControllerWin : ObjectViewController<DetailView, Contact> {
protected override void OnActivated() {
base.OnActivated();
ControlViewItem item = ((DetailView)View).FindItem("MyButton") as ControlViewItem;
if (item != null) {
item.ControlCreated = item_ControlCreated;
}
}
void item_ControlCreated(object sender, EventArgs e) {
(((ControlViewItem)sender).Control as Button).Text = "Click me!";
(((ControlViewItem)sender).Control as Button).Click = ButtonClickHandlingWinController_Click;
}
void ButtonClickHandlingWinController_Click(object sender, EventArgs e) {
MessageBox.Show("Action from custom View Item was executed!");
}
}
Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Layout
Imports DevExpress.ExpressApp.Win.Core
Imports System.Windows.Forms
' ...
Public Class ControlViewItemControllerWin
Inherits ObjectViewController(Of DetailView, Contact)
Protected Overrides Sub OnActivated()
MyBase.OnActivated()
Dim item As ControlViewItem = TryCast(CType(View, DetailView).FindItem("MyButton"), ControlViewItem)
If item IsNot Nothing Then
AddHandler item.ControlCreated, AddressOf item_ControlCreated
End If
End Sub
Private Sub item_ControlCreated(ByVal sender As Object, ByVal e As EventArgs)
TryCast(CType(sender, ControlViewItem).Control, Button).Text = "Click me!"
AddHandler TryCast(CType(sender, ControlViewItem).Control, Button).Click, AddressOf ButtonClickHandlingWinController_Click
End Sub
Private Sub ButtonClickHandlingWinController_Click(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("Action from custom View Item was executed!")
End Sub
End Class
ASP.NET
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Layout;
using DevExpress.ExpressApp.Web;
using DevExpress.Web;
// ...
public class ControlViewItemControllerWeb : ObjectViewController<DetailView, Contact> {
protected override void OnActivated() {
base.OnActivated();
ControlViewItem item = ((DetailView)View).FindItem("MyButton") as ControlViewItem;
if (item != null) {
item.ControlCreated = item_ControlCreated;
}
}
private void item_ControlCreated(object sender, EventArgs e) {
ASPxButton button = ((ControlViewItem)sender).Control as ASPxButton;
if (button == null) return;
button.ID = "MyButton";
button.Text = "Click me!";
button.Click = ButtonClickHandlingWebController_Click;
}
void ButtonClickHandlingWebController_Click(object sender, EventArgs e) {
WebWindow.CurrentRequestWindow.RegisterClientScript(
"ShowAlert", @"alert('Action from custom View Item was executed!');");
}
}
Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Layout
Imports DevExpress.ExpressApp.Web
Imports DevExpress.Web
' ...
Public Class ControlViewItemControllerWeb
Inherits ObjectViewController(Of DetailView, Contact)
Protected Overrides Sub OnActivated()
MyBase.OnActivated()
Dim item As ControlViewItem = TryCast(CType(View, DetailView).FindItem("MyButton"), ControlViewItem)
If item IsNot Nothing Then
AddHandler item.ControlCreated, AddressOf item_ControlCreated
End If
End Sub
Private Sub item_ControlCreated(ByVal sender As Object, ByVal e As EventArgs)
Dim button As ASPxButton = TryCast(CType(sender, ControlViewItem).Control, ASPxButton)
If button Is Nothing Then
Return
End If
button.ID = "MyButton"
button.Text = "Click me!"
AddHandler button.Click, AddressOf ButtonClickHandlingWebController_Click
End Sub
Private Sub ButtonClickHandlingWebController_Click(ByVal sender As Object, ByVal e As EventArgs)
WebWindow.CurrentRequestWindow.RegisterClientScript( _
"ShowAlert", "alert('Action from custom View Item was executed!');")
End Sub
End Class
移动的
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Layout;
using DevExpress.ExpressApp.Mobile.MobileModel;
using System;
//...
public class ControlViewItemControllerMobile : ObjectViewController<DetailView, Contact> {
protected override void OnActivated() {
base.OnActivated();
ControlViewItem item = ((DetailView)View).FindItem("MyButton") as ControlViewItem;
if (item != null) {
item.ControlCreated = item_ControlCreated;
}
}
private void item_ControlCreated(object sender, EventArgs e) {
Button button = ((ControlViewItem)sender).Control as Button;
if (button == null) return;
button.Text = "Click me!";
button.OnClick = @"DevExpress.ui.notify({
message: 'Action from custom View Item was executed!'
})";
}
}
Imports Microsoft.VisualBasic
Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Layout
Imports DevExpress.ExpressApp.Mobile.MobileModel
'...
Public Class ControlViewItemControllerMobile
Inherits ObjectViewController(Of DetailView, Contact)
Protected Overrides Sub OnActivated()
MyBase.OnActivated()
Dim item As ControlViewItem = TryCast(CType(View, DetailView).FindItem("MyButton"), _
ControlViewItem)
If item IsNot Nothing Then
AddHandler item.ControlCreated, AddressOf item_ControlCreated
End If
End Sub
Private Sub item_ControlCreated(ByVal sender As Object, ByVal e As EventArgs)
Dim button As Button = TryCast(DirectCast(sender, ControlViewItem).Control, Button)
If button Is Nothing Then
Return
End If
button.Text = "Click me!"
button.OnClick = "DevExpress.ui.notify({ " & ControlChars.CrLf & _
"message: 'Action from custom View Item was executed!' " & ControlChars.CrLf & _
"})"
End Sub
End Class