DevExpress Note

Last updated on 3 months ago

DevExpress Note

1.GridControl VS SeachLookUpEdit

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using DevExpress.XtraEditors.Repository;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Dev10_2_8
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}

private List<Objectes> list = new List<Objectes>();

private List<SeachLookUpEditData> SLEPLIST = new List<SeachLookUpEditData>();

private List<SeachLookUpEditData> SLESLIST = new List<SeachLookUpEditData>();

private void Form4_Load(object sender, EventArgs e)
{
Init();
}

public void Init()
{
#region 构建基础测试数据

list.Add(new Objectes
{
ID = "1",
NAME = "张三",
CLASSP = "a",
CLASSSON = "a1"
});

list.Add(new Objectes
{
ID = "2",
NAME = "李四",
CLASSP = "b",
CLASSSON = "b1"
});

#endregion

#region 构建SeachLookUpedit 数据源

SLEPLIST.Add(new SeachLookUpEditData
{
ID = "a",
NAME = "父节点A",
PID = "0"
});

SLEPLIST.Add(new SeachLookUpEditData
{
ID = "b",
NAME = "父节点B",
PID = "0"
});

SLESLIST.Add(new SeachLookUpEditData
{
ID = "a1",
NAME = "子节点A1",
PID = "a"
});

SLESLIST.Add(new SeachLookUpEditData
{
ID = "a2",
NAME = "子节点A2",
PID = "a"
});

SLESLIST.Add(new SeachLookUpEditData
{
ID = "b1",
NAME = "子节点B1",
PID = "b"
});

SLESLIST.Add(new SeachLookUpEditData
{
ID = "b2",
NAME = "子节点B2",
PID = "b"
});

#endregion

#region 绑定

this.repositoryItemSearchLookUpEdit1.DataSource = SLEPLIST;

this.repositoryItemSearchLookUpEdit2.DataSource = SLESLIST;

this.gridControl1.DataSource = list;

#endregion
}

/// <summary>
/// 关键方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void gridView1_CustomRowCellEditForEditing(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
{
if (e.Column.FieldName == "CLASSSON")
{
object oid = gridView1.GetFocusedRowCellValue("CLASSP");

if (oid == null) return;

string Pid = oid.ToString();

RepositoryItemSearchLookUpEdit lookup = ReturnLook(Pid);

e.RepositoryItem = lookup;
}
}

/// <summary>
/// 生产控件
/// </summary>
/// <param name="Pid"></param>
/// <returns></returns>
private RepositoryItemSearchLookUpEdit ReturnLook(string Pid)
{
var list = SLESLIST.Where(p => p.PID == Pid).ToList();

RepositoryItemSearchLookUpEdit obj = new RepositoryItemSearchLookUpEdit
{
ValueMember = "ID",

DisplayMember = "NAME",

DataSource = list
};

return obj;
}

/// <summary>
/// 防止选择完父节点后,对应的子节点字段与父节点字段冲突 比如 选择的父节点是b,但是这个时候子节点还是 a的子节点。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void repositoryItemSearchLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
var val = ((DevExpress.XtraEditors.BaseEdit)sender).EditValue;

if (val != null)
{
object Sid = gridView1.GetFocusedRowCellValue("CLASSSON");

var list = SLESLIST.Where(p => p.PID == val.ToString() && p.ID == Sid.ToString()).ToList();

if (list.Count == 0)
{
gridView1.SetFocusedRowCellValue("CLASSSON", null);
}
}
}
}
}

实体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Objectes
{
public string ID { get; set; }

public string NAME { get; set; }

public string CLASSP { get; set; }

public string CLASSSON { get; set; }
}

public class SeachLookUpEditData
{
public string ID { get; set; }

public string NAME { get; set; }

public string PID { get; set; }
}

结果

效果

2.DevExpress GridControl CardView

添加一个GridControl

ConvertCardView

GridViewCardView

CardView

设置字段

设置

代码

1
2
3
4
5
6
7
8
9
// 实体类
public class PicEntity
{
public string FILENAME { get; set; }

public string FILEPATH { get; set; }

public Byte[] PIC { get; set; }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/// <summary>
/// 选择图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Btn_ChoosePic_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog
{
Multiselect = true,

Title = "请选择文件",

Filter = "图片|*.jpg;*.png;*.gif;*.jpeg;*.bmp" //设置要选择的文件的类型
};

List<PicEntity> list = new List<PicEntity>();

if (fileDialog.ShowDialog() == DialogResult.OK)
{
foreach (string file in fileDialog.FileNames)
{
string[] arr = file.Split('\\');

using (Image img = Image.FromFile(file))
{
list.Add(new PicEntity { FILENAME = arr[arr.Length - 1], FILEPATH = file, PIC = FileContent(file) });//arr[arr.Length - 1]
}
}

gridControl1.DataSource = list;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/// <summary>
/// 文件转Byte数组
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
private byte[] FileContent(string fileName)
{
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
try
{
byte[] buffur = new byte[fs.Length];
fs.Read(buffur, 0, (int)fs.Length);
return buffur;
}
catch (Exception ex)
{
throw ex;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
/// <summary>
/// 构造函数
/// </summary>
public UC_PicList()
{
InitializeComponent();

//自适应高度
cardView1.OptionsBehavior.FieldAutoHeight = true;

//设置图片控件高度
repositoryItemPictureEdit1.CustomHeight = 300;
}

成品

选择图片文件,把图片信息放到数据源中,展示到视图中。

image-20211106161313136

3.Winform 动态设置控件属性

本文用到的是 Winform + Dev 控件

主要功能:可以读取配置(从数据库或者配置文件中读取),通过配置控制前面控件的属性

放点控件

先准备用到的实体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

/// <summary>
/// 控件配置
/// </summary>
public class ControlList
{
/// <summary>
/// 控件NAME
/// </summary>
public string ControlName { get; set; }
/// <summary>
/// 控件类型
/// </summary>
public string ControlTypeName { get; set; }
/// <summary>
/// 控件默认值
/// </summary>
public string ControlDefaultVal { get; set; }
/// <summary>
/// 控件可用
/// </summary>
public bool ControlEnable { get; set; }
/// <summary>
/// 控件显示
/// </summary>
public ControlvisibleInfo ControlVisible { get; set; }
}

public class ControlvisibleInfo
{
/// <summary>
/// 显示值
/// </summary>
public DevExpress.XtraLayout.Utils.LayoutVisibility IsVisible { get; set; }

/// <summary>
/// 包控件的Item
/// </summary>
public string PatientItem { get; set; }
}

正文

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}

private void Form5_Load(object sender, EventArgs e)
{
BindData();

SetControlsPropertyVal();
}

public List<ControlList> listName = new List<ControlList>();

public void SetControlsPropertyVal()
{
foreach (ControlList item in listName)
{
Control[] ctrols = layoutControl1.Controls.Find(item.ControlName, true);

if (ctrols.Length > 0)
{
Type t = ctrols[0].GetType();

if (t.Name == item.ControlTypeName && ctrols[0].Name == item.ControlName)
{
object o = t.GetProperty("EditValue").GetValue(ctrols[0], null);

t.GetProperty("EditValue").SetValue(ctrols[0], item.ControlDefaultVal);

t.GetProperty("Enabled").SetValue(ctrols[0], item.ControlEnable);

if (item.ControlVisible != null)
{
DevExpress.XtraLayout.BaseLayoutItem obj = layoutControlGroup1.Items.FindByName(item.ControlVisible.PatientItem);

if (obj != null)
{
obj.Visibility = item.ControlVisible.IsVisible;
}
}
}
}
}
}

public void BindData()
{
#region 绑定控件
List<SeachLookUpEditDataBind> list = new List<SeachLookUpEditDataBind>
{
new SeachLookUpEditDataBind { ID = "1", NAME = "张三" },
new SeachLookUpEditDataBind { ID = "2", NAME = "李四" },
new SeachLookUpEditDataBind { ID = "3", NAME = "王五" },
new SeachLookUpEditDataBind { ID = "4", NAME = "赵六" }
};

searchLookUpEdit1.Properties.ValueMember = "ID";
searchLookUpEdit1.Properties.DisplayMember = "NAME";
searchLookUpEdit1.Properties.DataSource = list;

searchLookUpEdit2.Properties.ValueMember = "ID";
searchLookUpEdit2.Properties.DisplayMember = "NAME";
searchLookUpEdit2.Properties.DataSource = list;
#endregion

#region 设置控件属性值

listName.Add(new ControlList
{
ControlName = "textEdit1",
ControlTypeName = "TextEdit",
ControlDefaultVal = "234",
ControlEnable = true,
ControlVisible = new ControlvisibleInfo
{
IsVisible = DevExpress.XtraLayout.Utils.LayoutVisibility.Always,
PatientItem = "layoutControlItem2"
}
});
listName.Add(new ControlList
{
ControlName = "searchLookUpEdit1",
ControlTypeName = "SearchLookUpEdit",
ControlDefaultVal = "1",
ControlEnable = false,
ControlVisible = new ControlvisibleInfo
{
IsVisible = DevExpress.XtraLayout.Utils.LayoutVisibility.Always,
PatientItem = "layoutControlItem1"
}
});
listName.Add(new ControlList
{
ControlName = "searchLookUpEdit2",
ControlTypeName = "SearchLookUpEdit",
ControlDefaultVal = "2",
ControlEnable = true,
ControlVisible = new ControlvisibleInfo
{
IsVisible = DevExpress.XtraLayout.Utils.LayoutVisibility.Never,
PatientItem = "layoutControlItem3"
}
});
listName.Add(new ControlList
{
ControlName = "radioGroup1",
ControlTypeName = "RadioGroup",
ControlDefaultVal = "1",
ControlEnable = true,
ControlVisible = new ControlvisibleInfo
{
IsVisible = DevExpress.XtraLayout.Utils.LayoutVisibility.Always,
PatientItem = "layoutControlItem4"
}
});

#endregion
}
}

4.Winform使用FontAwesome字体库

方式1

Nuget FontAwesomeNet

image-20211119113023849

界面

放 Dev的俩 simpleButton 和一个 imageCollection

image-20211119113135384

代码

构造函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public UC_UseFontAwesomeNet()
{
InitializeComponent();

// get FontAwesome icon class names(type is Dictionary<string, int>)
string[] names = FontAwesome.TypeDict.Select(v => v.Key).ToArray();

// use FontAwesome icon class name get FontAwesome icon Unicode value
int val = FontAwesome.TypeDict["fa-heart"];//0xf004

// defalut:
Bitmap bmp = FontAwesome.GetImage(val);//0xf004
Icon ico = FontAwesome.GetIcon(val);//0xf004

// custom:
FontAwesome.IconSize = 128;//change icon size
FontAwesome.ForeColer = Color.Purple;//change icon forecolor
Bitmap bmp1 = FontAwesome.GetImage(val);//0xf004
Icon ico1 = FontAwesome.GetIcon(val);//0xf004

imageCollection1.AddImage(bmp);
imageCollection1.AddImage(bmp1);

simpleButton1.ImageList = imageCollection1;
simpleButton2.ImageList = imageCollection1;

simpleButton1.ImageIndex = 0;
simpleButton2.ImageIndex = 1;
}

结果

成果

查看图标编码

如下两个地址都可以。

https://www.bootcss.com/p/font-awesome/design.html
http://www.fontawesome.com.cn/cheatsheet/

需要将 &#x 替换成 \u 最终是\ue603

方式2

添加按钮

通方式1中一样,都用imageCollection 去指向

再添加一个按钮

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public UC_UseFontAwesomeNet()
{
InitializeComponent();

// get FontAwesome icon class names(type is Dictionary<string, int>)
string[] names = FontAwesome.TypeDict.Select(v => v.Key).ToArray();

// use FontAwesome icon class name get FontAwesome icon Unicode value
int val = FontAwesome.TypeDict["fa-heart"];//0xf004

// defalut:
Bitmap bmp = FontAwesome.GetImage(val);//0xf004
Icon ico = FontAwesome.GetIcon(val);//0xf004

// custom:
FontAwesome.IconSize = 128;//change icon size
FontAwesome.ForeColer = Color.Purple;//change icon forecolor
Bitmap bmp1 = FontAwesome.GetImage(val);//0xf004
Icon ico1 = FontAwesome.GetIcon(val);//0xf004

imageCollection1.AddImage(bmp);
imageCollection1.AddImage(bmp1);

simpleButton1.ImageList = imageCollection1;
simpleButton2.ImageList = imageCollection1;

simpleButton1.ImageIndex = 0;
simpleButton2.ImageIndex = 1;
// ***********************上面使用的是FontAwesomeNet****************
// ***********************simpleButton3 使用转换方法****************
//simpleButton3
Image img = GetFontImage("\uf2cd", Color.Red, 20);
imageCollection1.AddImage(img);
simpleButton3.ImageList = imageCollection1;
simpleButton3.ImageIndex = 2;

// ***********************这里使用的是直接设置**********************************
//PrivateFontCollection pfc = new PrivateFontCollection();

//string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "font/fontawesome-webfont.ttf");

//pfc.AddFontFile(path);

//simpleButton3.Text = "\uf2cd";

//simpleButton3.Font = new Font(pfc.Families[0], 20);

//simpleButton3.ForeColor = Color.Red;

}

帮助转换方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//https://www.bootcss.com/p/font-awesome/design.html  图标代码
//http://www.fontawesome.com.cn/cheatsheet/ 图标代码
/// <summary>
/// 通过FontAwesome设置字体
/// </summary>
public class FontAwesomeHelper
{
/// <summary>
/// 字体图标生成图标
/// </summary>
/// <param name="fontIco">图标编码</param>
/// <param name="color">颜色</param>
/// <param name="size">大小</param>
/// <returns></returns>
public static Image GetFontImage(string fontIco, Color color, int size)
{
Bitmap bmp = new Bitmap(size, size);
Graphics g = Graphics.FromImage(bmp);
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;

string ch = fontIco;
Font font = GetAdjustedFont(g, ch, size, size, 4, true);
SizeF stringSize = g.MeasureString(ch, font, size);
float w = stringSize.Width;
float h = stringSize.Height;
float left = (size - w) / 2;
float top = (size - h) / 2;
// Draw string to screen.
SolidBrush brush = new SolidBrush(color);
g.DrawString(ch, font, brush, new PointF(left, top));
return bmp;
}

/// <summary>
/// 找字体图片
/// </summary>
/// <param name="g">图片矢量</param>
/// <param name="graphicString">图标编码</param>
/// <param name="containerWidth"></param>
/// <param name="maxFontSize">最大Size</param>
/// <param name="minFontSize">最小Size</param>
/// <param name="smallestOnFail">找不到最小找最大</param>
/// <returns></returns>
private static Font GetAdjustedFont(Graphics g, string graphicString, int containerWidth, int maxFontSize, int minFontSize, bool smallestOnFail)
{
for (double adjustedSize = maxFontSize; adjustedSize >= minFontSize; adjustedSize = adjustedSize - 0.5)
{
Font testFont = GetIconFontFromResource((float)adjustedSize);
SizeF adjustedSizeNew = g.MeasureString(graphicString, testFont);
if (containerWidth > Convert.ToInt32(adjustedSizeNew.Width))
{
return testFont;
}
}
return GetIconFontFromResource(smallestOnFail ? minFontSize : maxFontSize);
}

/// <summary>
/// 去文件夹中去找字体文件
/// </summary>
/// <param name="size"></param>
/// <returns></returns>
private static Font GetIconFontFromFilePath(float size)
{
PrivateFontCollection pfc = new PrivateFontCollection();

string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "font/fontawesome-webfont.ttf");

pfc.AddFontFile(path);

return new Font(pfc.Families[0], size, GraphicsUnit.Point);
}

/// <summary>
/// 去项目资源中去找字体文件
/// 记录坑,fontawesome-webfont.ttf 加到资源中名称变成 fontawesome_webfont里
/// </summary>
/// <param name="size"></param>
/// <returns></returns>
private static Font GetIconFontFromResource(float size)
{
PrivateFontCollection pfc = new PrivateFontCollection();

Assembly myAssem = Assembly.GetEntryAssembly();

ResourceManager rm = new ResourceManager(myAssem.GetName().Name.ToString() + ".Properties.Resources", myAssem);

byte[] rgbyt = (byte[])rm.GetObject("fontawesome_webfont");

IntPtr pbyt = Marshal.AllocCoTaskMem(rgbyt.Length);

if (null != pbyt)
{
Marshal.Copy(rgbyt, 0, pbyt, rgbyt.Length);

pfc.AddMemoryFont(pbyt, rgbyt.Length);

Marshal.FreeCoTaskMem(pbyt);

return new Font(pfc.Families[0], size, GraphicsUnit.Point);
}
else
{
return null;
}
}

结果

成果


DevExpress Note
http://example.com/2021/10/30/DevExpress-Note/
Author
Harris
Posted on
October 30, 2021
Licensed under