DevExpress_常用控件入门

简介:

XtraEditors   Library是专门为可视化的Studio.NET设计的最优化的一套100%的C#控件

XtraEdiotors Library是一款具有革命性的超过20种数据编辑控制的控件

它是国内第一款适合于.NET框架类型的控件。


准备工作

1. DevExpress控件的安装
2. Demo查看
3. 帮助文档使用


与.net基本的控件比较

1. 命名空间(NameSpace)
.net基本控件的类都在System.Windows.Forms的命名空间下
DevExpress的控件类在DevExpress命名空间下


2. 可以代替.net的控件
DevExpress的大部分控件都已可以代替.net的基本控件。


如:

按钮:
System.Windows.Forms.Button -> DevExpress.XtraEditors.SimpleButton


文本框:
System.Windows.Forms.TextBox -> DevExpress.XtraEditors.TextEdit

复选框
System.Windows.Forms.CheckBox -> DevExpress.XtraEditors.CheckEdit

下拉框:
System.Windows.Forms.ComboBox -> DevExpress.XtraEditors.ComboBoxEdit


日 期:
System.Windows.Forms.DateTimePicker -> DevExpress.XtraEditors.DateEdit   /   DevExpress.XtraEditors.TimeEdit

这里就不一一列举了,认真看看,相信一定找出很多可以替代的控件


二. 几个比较重要、常用的属性

1:EditValue

DevExpress.XtraEditors.*Edit 的控件都不可少的一个EditValue属性。

如:DevExpress.XtraEditors.*Edit

通常,EditValue会跟Text的值是一样的。


只是EditValue的类型为Object,

Text的属性为String,

也就是EditValue通常可以代替Text属性。


2. Enable 和 Visable

是否禁用和是否可见


3. Properties

设置控件一些特征
DevExpress.XtraEditors.TextEdit   textEdit  =  …;

例如 是否只读:
textEdit.Properties.ReadOnly = true;


不允许获得焦点
textEdit.Properties.AllowFocused = false;


禁止空值输入
textEdit.Properties.AllowNullInput = false;

// 当这个属性应用在TimeEdit中,它的清除按钮,将会禁用(灰掉)喵喵喵???


禁止编辑器输入

ComboBoxEdit   comboBoxEdit  =  …;
comboBoxEdit.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;

// 只选模式,不能输入文本


4. Appearance 设置风格。

Dexpress把所有设置控件风格的特性都放到Appearance属性下。
例:
DevExpress.XtraEditors.SimpleButton  simpleButton = …;

//前景色
simpleButton.Appearance.ForeColor = Color.Red;

//背景色
simpleButton.Appearance.BackColor = Color.Red;


Appearance.TextOptions
文本对齐操作

// 居中对齐
simpleButton.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

// 自动换行。

// 当控件的宽度容不下文本的长度,会自动换行。
simpleButton.Appearance.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;


注意:

在某些控件中, Properties属性下才有Apperarance
DevExpress.XtraEditors.TextEdit  textEdit =  …;
textEdit.Properties.Appearance.ForeColor = Color.Red;


三. 几个常用的控件

1:用强大的LookUpEdit代替ComboBox

1.1 ComboBox不支持数据绑定
2.1 由于DevExpress的ComboBox天生存在数据绑定缺陷,

所以有时我们要做数据绑定这种杀鸡小事时,不得不使用牛刀LooUpEdit


如下代码,可用实现一个ComboBox:

// 未闻花名vwhm.net
// 禁止文本输入
this.lookUpEdit1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
// 默认为null的显示
this.lookUpEdit1.Properties.NullText = "[请选择类别]";
// 加入一个显示列
this.lookUpEdit1.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Name"));
// 不显示页眉(包括列头)
this.lookUpEdit1.Properties.ShowHeader = false;
// 不显示页脚(包括关闭按钮)
this.lookUpEdit1.Properties.ShowFooter = false;
// 要显示的字段,Text获得
this.lookUpEdit1.Properties.DisplayMember = "Name";
// 实际值的字段,EditValue获得
this.lookUpEdit1.Properties.ValueMember = "Value";
// 数据绑定 
ICollection list = Global.ClassCategoryList;
// 绑定数据
this.lookUpEdit1.Properties.DataSource = list;
// 设置行数(根据这个自动设置高度)
this.lookUpEdit1.Properties.DropDownRows = list.Count;


2. GridControl

GridControl可以代替.net的System.Windows.Forms.DataGrid控件。
GirdControl只是一个容器控件,必须要求GridView视图作为它的子控件。
GridControl可以包含多个视图,可以实现视图的切换


每个GridView视图必须包含列(Column)
GridControl支持层级视图


GridControl 经常设置的属性
使用导航器

this.gridControl1.UseEmbeddedNavigator = true;

this.gridControl1.EmbeddedNavigator.Buttons.Append.Visible = false;
this.gridControl1.EmbeddedNavigator.Buttons.CancelEdit.Visible = false;
this.gridControl1.EmbeddedNavigator.Buttons.Edit.Visible = false;
this.gridControl1.EmbeddedNavigator.Buttons.EndEdit.Visible = false;
this.gridControl1.EmbeddedNavigator.Buttons.Remove.Visible = false;

this.gridControl1.EmbeddedNavigator.Buttons.First.Visible = true;
this.gridControl1.EmbeddedNavigator.Buttons.Last.Visible = true;
this.gridControl1.EmbeddedNavigator.Buttons.Next.Visible = true;
this.gridControl1.EmbeddedNavigator.Buttons.NextPage.Visible = true;
this.gridControl1.EmbeddedNavigator.Buttons.Prev.Visible = true;
this.gridControl1.EmbeddedNavigator.Buttons.PrevPage.Visible = true;



GridView 经常设置的属性

// 禁止编辑
this.gridView1.OptionsBehavior.Editable = false;

// 不允许使用过滤
this.gridView1.OptionsCustomization.AllowFilter = false;

// 不允许使用排序
this.gridView1.OptionsCustomization.AllowSort = false;

// 不显示组面板
this.gridView1.OptionsView.ShowGroupPanel = false;

// 如果宽度溢出,自动出现滚动条

this.gridView1.OptionsView.ColumnAutoWidth =true;

// 禁止单元格获得焦点
this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;

// 选择的行背景透明
this.gridView1.Appearance.SelectedRow.BackColor = Color.Transparent;

事件

// 未闻花名vwhm.net
//订阅行焦点改变事件
this.gridView1.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(this.gridView1_FocusedRowChanged);

//验证编辑器(单元格)值输入
this.gridView1.ValidatingEditor += new DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventHandler(this.gridView1_ValidatingEditor);

private void gridView1_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
{
 if (gridView1.FocusedColumn == col产品名称)
 {
     if (string.IsNullOrEmpty(e.Value as string)) {
         e.ErrorText = "产品名称不能为空";
         e.Valid = false;
     }
 }
}


 
 
// 未闻花名vwhm.net
//订阅设置行风格事件
this.gridView1.RowStyle += new DevExpress.XtraGrid.Views.Grid.RowStyleEventHandler(this.gridView1_RowStyle);

private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
{
 object value = gridView1.GetRowCellValue(e.RowHandle, "中止");
 if (value!=null&&(bool)value) {
 e.Appearance.ForeColor = Color.Red;
 }
}

this.gridView1.CustomColumnDisplayText += new DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventHandler(this.gridView1_CustomColumnDisplayText);

private DataTable _CategoryList;

public DataTable CategoryList {
 get {
 if (_CategoryList == null) {
 _CategoryList=GetData("select * from 产品类别");
 DataColumn pk = _CategoryList.Columns["类别ID"];
 _CategoryList.PrimaryKey = new DataColumn[] {pk};
 }
 return _CategoryList;
 }
}

private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
{
 if (e.Column.Name == "col类别ID") {
 e.DisplayText = CategoryList.Rows.Find(e.Value)["类别名称"] as string;
 }
}

// 行单元格对齐
this.gridView1.RowCellDefaultAlignment += new DevExpress.XtraGrid.Views.Base.RowCellAlignmentEventHandler(this.gridView1_RowCellDefaultAlignment);

private void gridView1_RowCellDefaultAlignment(object sender, DevExpress.XtraGrid.Views.Base.RowCellAlignmentEventArgs e)
{
 e.HorzAlignment = DevExpress.Utils.HorzAlignment.Near;
}

3. 界面操作

3.1 根据条件操作行或列风格

3.2 添加RepositoryItem(内嵌元素In-place EditorRepository)

3.3 列汇总

首先,设置this.gridView1.OptionsView.ShowFooter = true;

this.col库存量.SummaryItem.DisplayFormat = “总量:{0}”;

// 六种:Sum,Average,Count,Max,Min,Custom
col单位数量.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;


四. 数据检查、验证

1. 演示Mask
2. DXValidationProvider的组件使用

猜你喜欢

转载自www.cnblogs.com/springsnow/p/10298913.html
今日推荐