1.主要操作
属性框基本上长这个样子
与GridControl一样,想要设置行需要添加相应的FiledName和一些控件,比如线路颜色我需要用ColorEdit这个控件
则在In-place Editor中间添加
DevExpress容器控件中想要添加的控件一般都是Repository里的
2.初始设置属性框控件
初始设置PropertyGridControl
/// <summary>
/// 初始设置PropertyGridControl
/// </summary>
void InitialSetPropCtrl()
{
checkBtnPropertySort.CheckedChanged += new EventHandler(checkBtnSort);
checkBtnAZSort.CheckedChanged += new EventHandler(checkBtnSort);
SetBarButtonToolTip(checkBtnPropertySort, "分组排序");
SetBarButtonToolTip(checkBtnAZSort, "按字母排序");
checkBtnPropertySort.Checked = true;
propertyGridControl1.ExpandAllRows();
propertyGridControl1.OptionsBehavior.PropertySort = DevExpress.XtraVerticalGrid.PropertySort.NoSort;
CustomAttribute ca1 = new CustomAttribute();//初始的自定义属性
propertyGridControl1.SelectedObject = ca1;
//
DevExpress.XtraVerticalGrid.Rows.BaseRow br = propertyGridControl1.GetRowByCaption("线路");
//通过循环遍历设置属性的中文名称
foreach (DevExpress.XtraVerticalGrid.Rows.EditorRow per in br.ChildRows)
{
if (per.ChildRows.Count > 0)
{ //利用递归解决多层可扩展属性的caption的赋值
SetCustomAttributeCaption(per);
}
string dicKey = per.Properties.FieldName;
if (CustomAttribute.dic.ContainsKey(dicKey))
per.Properties.Caption = CustomAttribute.dic[dicKey];
per.Height = 23;//设置属性行高度
}
}
3.同时自定义了一个CustomAttribute类
/// <summary>
/// 自定义属性类
/// </summary>
public class CustomAttribute
{
/// <summary>
/// 用于存放属性英文-中文的键值对
/// </summary>
public static Dictionary<string, string> dic = new Dictionary<string, string>();
///初始值,其实可以直接在属性里写默认值
private GMap.NET.WindowsForms.Markers.GMarkerGoogleType _LineStyle= GMap.NET.WindowsForms.Markers.GMarkerGoogleType.none;
private string _LineName = "Unknown";
private int _PointCount = 0;
private bool _IsVisible = false;
private Color _LineColor = Color.Blue;
private bool _IsEditable = false;
public CustomAttribute()
{
dic.Add("LineStyle", "线路标注样式");
dic.Add("PointCount", "线路点个数");
dic.Add("LineName", "线路名称");
dic.Add("IsVisible", "是否隐藏");
dic.Add("LineColor", "线路颜色");
dic.Add("IsEditable", "是否可移动");
}
public CustomAttribute(string LineName,int PointCount,bool IsVisible,GMap.NET.WindowsForms.Markers.GMarkerGoogleType type,Color lineColor,bool isEditable)
{
this.LineName = LineName;
this.PointCount = PointCount;
this.IsVisible = IsVisible;
this.LineStyle = type;
this.LineColor = lineColor;
this.IsEditable = isEditable;
}
//public CustomAttribute()
/// <summary>
/// 线路标注样式
/// </summary>
[Browsable(true), Category("线路")]
public GMap.NET.WindowsForms.Markers.GMarkerGoogleType LineStyle
{
get {return _LineStyle; }
set {_LineStyle=value; }
}
/// <summary>
/// 线路名称
/// </summary>
[Browsable(true), Category("线路")]
public string LineName
{
get {return _LineName; }
set {_LineName=value; }
}
/// <summary>
/// 是否隐藏
/// </summary>
[Browsable(true), Category("线路")]
public bool IsVisible
{
get { return _IsVisible; }
set
{
_IsVisible = value;
}
}
/// <summary>
/// 线路点个数
/// </summary>
[Browsable(true), Category("线路")]
public int PointCount
{
get {return _PointCount; }
set {_PointCount=value; }
}
[Browsable(true), Category("线路")]
public Color LineColor
{
get { return _LineColor; }
set { _LineColor = value; }
}
[Browsable(true), Category("线路")]
public bool IsEditable
{
get { return _IsEditable; }
set { _IsEditable = value; }
}
///// <summary>
///// 可展开属性
///// </summary>
///// TypeConverter(typeof(ExpandableObjectConverter)):将CustomAttribute1类型的对象转为可扩展对象
//[Browsable(true), Category("自定义属性"), TypeConverter(typeof(ExpandableObjectConverter))]
//public CustomAttribute1 CA1
//{
// get { return _ca1; }
// set
// {
// _ca1 = value;
// }
//}
}
///// <summary>
///// 自定义属性类1
///// </summary>
//class CustomAttribute1
//{
// public CustomAttribute1()
// {
// CustomAttribute.dic.Add("CustomDisplayFormart", "显示类型");
// CustomAttribute.dic.Add("CustomFormartString", "类型格式");
// }
// private CustomAttribute2 _CustomDisplayFormart;
// private string _CustomFormartString = "";
// /// <summary>
// /// 显示类型
// /// </summary>
// [Browsable(true), TypeConverter(typeof(ExpandableObjectConverter))]
// public CustomAttribute2 CustomDisplayFormart
// {
// get { return _CustomDisplayFormart; }
// set
// {
// _CustomDisplayFormart = value;
// }
// }
// /// <summary>
// /// 类型格式
// /// </summary>
// public string CustomFormartString
// {
// get { return _CustomFormartString; }
// set
// {
// _CustomFormartString = value;
// }
// }
//}
///// <summary>
///// 自定义属性类2
///// </summary>
//class CustomAttribute2
//{
// public CustomAttribute2()
// {
// CustomAttribute.dic.Add("CustomDisplayFormartValue", "显示格式值");
// CustomAttribute.dic.Add("AllowUseCustomDisplayFormart", "是否启用显示格式");
// }
// private string _CustomDisplayFormartValue = "";
// private CustomAttribute.VisibleStatus _AllowUseCustomDisplayFormart = CustomAttribute.VisibleStatus.False;
// /// <summary>
// /// 值
// /// </summary>
// public string CustomDisplayFormartValue
// {
// get { return _CustomDisplayFormartValue; }
// set
// {
// _CustomDisplayFormartValue = value;
// }
// }
// /// <summary>
// /// 是否启用
// /// </summary>
// public CustomAttribute.VisibleStatus AllowUseCustomDisplayFormart
// {
// get { return _AllowUseCustomDisplayFormart; }
// set
// {
// _AllowUseCustomDisplayFormart = value;
// }
// }
//}
4.一些方法
把自定义属性CustomAttribute中的Dictionary键值加入到EditorRow里,Caption和Value
这里是添加初始属性框的描述、添加自定义属性、设置当前属性
/// <summary>
/// 设置自定义属性的描述
/// </summary>
private void SetCustomAttributeCaption(DevExpress.XtraVerticalGrid.Rows.EditorRow EditorRow)
{
foreach (DevExpress.XtraVerticalGrid.Rows.EditorRow per_child in EditorRow.ChildRows)
{
if (per_child.ChildRows.Count > 0)
{
//利用递归解决多层可扩展属性的caption的赋值
SetCustomAttributeCaption(per_child);
}
//FieldName属性包含了该属性的父属性FieldName;通过 . 分割
string[] per_child_FieldName = per_child.Properties.FieldName.Split('.');
string dicKey = per_child_FieldName[per_child_FieldName.GetLength(0) - 1];
if (CustomAttribute.dic.ContainsKey(dicKey))
per_child.Properties.Caption = CustomAttribute.dic[dicKey];
per_child.Height = 23;//设置属性行高度
}
}
/// <summary>
/// 添加到全局变量的CustomAttributes
/// </summary>
/// <param name="overlay">图层</param>
/// <param name="IsSetPropCtrl">是否添加到属性框里</param>
/// <param name="isVisible">是否隐藏这一栏</param>
private void AddCustomAttribute(GMapOverlay overlay, bool IsSetPropCtrl, bool isVisible, System.Drawing.Color color, bool isEditable)
{
CustomAttribute attribute = new CustomAttribute(overlay.Id, overlay.Markers.Count, isVisible, ((GMarkerGoogle)overlay.Markers[0]).Type, color, isEditable);
customAttributes.Add(attribute);
if (!isEditable)//不可编辑即锁定状态
{ chooseLocked.Add(overlay.Id); }
currentAttribute = attribute;
if (IsSetPropCtrl)
{
SetCurrentCustomAttribute(overlay.Id);
}
}
/// <summary>
/// 设置当前的自定义属性
/// </summary>
/// <param name="overlayname">当前图层的名称</param>
private void SetCurrentCustomAttribute(string overlayname)
{
var currentCustom = customAttributes.Find(a => a.LineName == overlayname);
this.propertyGridControl1.SelectedObject = currentCustom;
currentAttribute = currentCustom;
//
DevExpress.XtraVerticalGrid.Rows.BaseRow br = propertyGridControl1.GetRowByCaption("线路");
//通过循环遍历设置属性的中文名称
//foreach (DevExpress.XtraVerticalGrid.Rows.PGridEditorRow per in br.ChildRows)
foreach (DevExpress.XtraVerticalGrid.Rows.EditorRow per in br.ChildRows)
{
if (per.ChildRows.Count > 0)
{ //利用递归解决多层可扩展属性的caption的赋值
SetCustomAttributeCaption(per);
}
string dicKey = per.Properties.FieldName;
if (CustomAttribute.dic.ContainsKey(dicKey))
per.Properties.Caption = CustomAttribute.dic[dicKey];
per.Height = 23;//设置属性行高度
}
}
5.事件
#region 属性框事件
//CheckButton.CheckeChange事件
void checkBtnSort(object sender, EventArgs e)
{
CheckButton thisChk = (CheckButton)sender;
if (thisChk == checkBtnPropertySort)
{
if (checkBtnPropertySort.Checked)
SetBarButtonDown(checkBtnAZSort, false);
else
SetBarButtonDown(checkBtnAZSort, true);
}
else
{
if (checkBtnAZSort.Checked)
SetBarButtonDown(checkBtnPropertySort, false);
else
SetBarButtonDown(checkBtnPropertySort, true);
}
UpdatePropertyGrid();
}
//设置按钮的鼠标悬浮气泡提示信息
static void SetBarButtonToolTip(CheckButton chkBtn, string value)
{
DevExpress.Utils.SuperToolTip superToolTip = new DevExpress.Utils.SuperToolTip();
DevExpress.Utils.ToolTipTitleItem toolTipTitleItem = new DevExpress.Utils.ToolTipTitleItem();
toolTipTitleItem.Text = value;
superToolTip.Items.Add(toolTipTitleItem);
chkBtn.SuperTip = superToolTip;
}
//设置按钮是否按下
void SetBarButtonDown(CheckButton chkBtn, bool value)
{
chkBtn.CheckedChanged -= new EventHandler(checkBtnSort);
chkBtn.Checked = value;
chkBtn.CheckedChanged += new EventHandler(checkBtnSort);
}
//更改控件排序方式
void UpdatePropertyGrid()
{
this.propertyGridControl1.OptionsView.ShowRootCategories = this.checkBtnPropertySort.Checked;
}
//属性框cell的值改变
private void propertyGridControl1_CellValueChanged(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e)
{
try
{
var rowFieldName = e.Row.Name.ToString();
switch (rowFieldName)
{
case "LineStyle":
ChangeLineStyle((GMarkerGoogleType)e.Value);
break;
case "IsVisible":
currentAttribute.IsVisible = (bool)e.Value;
break;
case "LineColor":
ChangeLineMarkerColor((System.Drawing.Color)e.Value);
break;
default:
break;
}
}
catch (Exception ex)
{
WriteLog(ex.Message);
XtraMessageBox.Show(ex.Message);
}
}
#endregion
#endregion
#region 更改线路标注或者颜色(xml文件和其他文件分开处理)
/// <summary>
///
/// </summary>
/// <param name="type"></param>
private void ChangeLineStyle(GMarkerGoogleType type)
{
var overlay = gMapOverlays.Find(a => a.Id == currentAttribute.LineName);
currentOverlay = overlay;
//改变customAttribute的属性
var customattribute = customAttributes.Find(a => a.LineName == currentAttribute.LineName);
customattribute.LineStyle = type;
if (overlay.Id.Contains(".xml"))//
{
List<GMarkerGoogleExt> points = new List<GMarkerGoogleExt>();
foreach (GMapMarker marker in overlay.Markers)
{
GMarkerGoogleExt newmarker = UpdateMarker(marker, type);
points.Add(newmarker);
}
overlay.Markers.Clear();
foreach (GMarkerGoogleExt a in points)
{
overlay.Markers.Add(a);
}
this.gmap.Refresh();
}
else
{
GMarkerGoogleExt pfirst = UpdateMarker(overlay.Markers[0], type);
GMarkerGoogleExt plast = UpdateMarker(overlay.Markers.Last(), type);
overlay.Markers.RemoveAt(0);
overlay.Markers.Insert(0, pfirst);
overlay.Markers.RemoveAt(overlay.Markers.IndexOf(overlay.Markers.Last()));
overlay.Markers.Add(plast);
this.gmap.Refresh();
}
}
/// <summary>
/// 改变线路文件的起始点和终点的Marker样式
/// </summary>
/// <param name="c">颜色</param>
private void ChangeLineMarkerColor(System.Drawing.Color c)
{
var overlay = gMapOverlays.Find(a => a.Id == currentAttribute.LineName);
var customattribute = customAttributes.Find(a => a.LineName == currentAttribute.LineName);
customattribute.LineColor = c;
if (!(overlay.Id.Contains(".xml")))//不是xml文件
{
for (int i = 1; i < overlay.Markers.Count - 1; i++)
{
GMarkerGoogleExt newmarker = UpdateMarker(overlay.Markers[i], c);
overlay.Markers.RemoveAt(i);
overlay.Markers.Insert(i, newmarker);
}
foreach (GMapRouteExt route in overlay.Routes)
{
route.Stroke = new Pen(c, 3);
}
this.gmap.Refresh();
}
else
{
foreach (GMapRouteExt route in overlay.Routes)
{
route.Stroke = new Pen(c, 3);
}
this.gmap.Refresh();
}
}
/// <summary>
/// 更新Marker的标注类型
/// </summary>
/// <param name="marker"></param>
/// <param name="type"></param>
/// <returns></returns>
private GMarkerGoogleExt UpdateMarker(GMapMarker marker, GMarkerGoogleType type)
{
GMarkerGoogleExt newmarker = new GMarkerGoogleExt(marker.Position, type);
newmarker.ToolTipText = marker.ToolTipText;
newmarker.ToolTip.Foreground = Brushes.Black;
newmarker.ToolTip.TextPadding = new Size(20, 10);
return newmarker;
}
/// <summary>
/// 更新Marker的颜色
/// </summary>
/// <param name="marker"></param>
/// <param name="c"></param>
/// <returns></returns>
private GMarkerGoogleExt UpdateMarker(GMapMarker marker, System.Drawing.Color c)
{
GMarkerGoogleExt newmarker = new GMarkerGoogleExt(marker.Position, ((GMarkerGoogle)marker).Type, c, 5, true);
newmarker.ToolTipText = marker.ToolTipText;
newmarker.ToolTip.Foreground = Brushes.Black;
newmarker.ToolTip.TextPadding = new Size(20, 10);
return newmarker;
}
/// <summary>
/// 更新Marker的位置和ToolTipText
/// </summary>
/// <returns></returns>
private GMarkerGoogleExt UpdateMarker(GMarkerGoogleExt marker, int index, PointLatLng point)
{
GMarkerGoogleExt newmarker = marker;
newmarker.Position = point;
newmarker.ToolTipText = string.Format("点编号{0}:经度{1},纬度{2}", index, point.Lng, point.Lat);
newmarker.ToolTip.Foreground = Brushes.Black;
newmarker.ToolTip.TextPadding = new Size(20, 10);
return marker;
}
#endregion