private void dataGridViewProperty_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (this._updateSelectedObject == false) { return; } if (this.propertyGridDataGridView.SelectedObjectSeting) { return; } //更新子行的值是,这个事件会触发两次,并不是一次父行一次子行触发 //而是在通知父行在更新值是,父行会自动更新其下子行,就又一次进来 //更新子行时子行去更新父行,不会使父行进入这个事件 //为了避免子行两次进来,增加一个valueChanging属性 //首先判断是否是一个实现了IPropertyGirdCell接口的属性编辑单元格 IPropertyGirdCell iPropertyGirdCell = this.propertyGridDataGridView[e.ColumnIndex, e.RowIndex] as IPropertyGirdCell; if (iPropertyGirdCell == null) { return; } PropertyGridRow propertyGirdRow = this.propertyGridDataGridView.Rows[e.RowIndex] as PropertyGridRow; //如果是自己引发的更新 if (propertyGirdRow.ValueChangingBySelf) { return; } //如果当前行是子行,设置ValueChangingBySelf=true,表示接下来由于 UpdateSelectedObject 再次进入的这个事件 //是自己引发的 //保证PropertyChanged委托只调用一次 if (propertyGirdRow.ParentRow != null) { propertyGirdRow.ValueChangingBySelf = true; } propertyGirdRow.ContrastDefaultValue(); propertyGirdRow.UpdateSelectedObject(); //调用属性已更改事件 if (this.PropertyChanged != null) { PropertyChangeEventArgs eventArgs = new PropertyChangeEventArgs(this.SelectedObjects, propertyGirdRow); PropertyChanged(sender, eventArgs); Debug.WriteLine("PropertyChanged > CellValueChanged : " + propertyGirdRow.PropertyName); } propertyGirdRow.ValueChangingBySelf = false; }
/// <summary> /// 从PropertyGirdCell中取出旧值 /// </summary> /// <returns></returns> public object GetPropertyOldValue() { IPropertyGirdCell iPropertyGirdCell = this._propertyGridCell as IPropertyGirdCell; if (iPropertyGirdCell == null) { throw new NotImplementedException(); } return(iPropertyGirdCell.GetPropertyOldValue()); }
/// <summary> /// 向属性行设置一个值 /// 仅在初始化属性行时调用,用于初始化属性行上的属性(Property)值 /// </summary> /// <param name="value"></param> public void SetPropertyValue(object value) { //注意 //进这个方法时,该行还没有被加到DataGridView中,此时还在行初始化阶段 //所以在这里调用任何和DataGridView有关的方法都是无效的 IPropertyGirdCell iPropertyGirdCell = this._propertyGridCell as IPropertyGirdCell; if (iPropertyGirdCell == null) { throw new NotImplementedException(); } iPropertyGirdCell.SetPropertyValue(value); //如果属性值是null,把可能包含的子属性行去掉 if (value == null) { #region 移除可能存在的子属性行 //移除可能存在的子属性行 if (this.SubRows != null && this.DataGridView != null) { IsCollapse = true; } this.SubRows = null; #endregion } else { #region 判断是否需要添加子属性行 //判断是否需要添加子属性行 //因为可能子属性行之前因为value为null被清掉了,在重新设置不为null的值后,还要再把它们加载进来 if (this.SubRows == null) { InitSubRow(); } #endregion } if (this.Index > -1) { this.DataGridView.InvalidateRow(this.Index); } //处理此属性行可能包含的子属性行 if (this.SubRows != null) { foreach (PropertyGridRow row in this.SubRows) { row.UpdateProperty(); } } //如此当前行是一个子属性行,需要通知它的父行重绘,以显示新的属性值 //但是注意可能这些行还没有被加载到DataGridView中 if (this.ParentRow != null && this.DataGridView != null) { this.DataGridView.InvalidateRow(this.ParentRow.Index); } }
/// <summary> /// 构造,应使用此构造,将SelectedObject的初始化留在后面处理,因为要兼容多对象同时选中 /// </summary> /// <param name="attribute"></param> /// <param name="propertyInfoName"></param> public PropertyGridRow(PropertyRelatorAttribute attribute, PropertyInfo propertyInfo, PropertyGridPad propertyGrid) { this._propertyRelatorAttribute = attribute; this._propertyInfo = propertyInfo; this._propertyGrid = propertyGrid; #region 初始化单元格 DataGridViewCell dcPropertyName = new DataGridViewTextBoxCell(); dcPropertyName.Value = attribute.PropertyDisplayName; this.Cells.Add(dcPropertyName); DataGridViewCell dcDescription = new DataGridViewTextBoxCell(); dcDescription.Value = attribute.Description; this.Cells.Add(dcDescription); #region 初始化用于编辑属性值的单元格 //获取PropertyEditorAttribute PropertyEditorAttribute[] editorAttribute = (PropertyEditorAttribute[])propertyInfo.GetCustomAttributes(typeof(PropertyEditorAttribute), true); if (editorAttribute == null || editorAttribute.Length == 0) { //没有加PropertyEditorAttribute的Attribute,分配一个默认的文本框做为编辑控件 //TODO:分配一个默认的文本框做为编辑控件 } else { //创建属性(Property)单元格 _propertyGridCell = CellFactory.Instance.Create(editorAttribute[0]) as DataGridViewCell; //TODO:这里要考虑没有创建出合适的cell的情况,既_propertyGridCell为null的情况 _iPropertyGirdCell = _propertyGridCell as IPropertyGirdCell; _iPropertyGirdCell.PropertyRelatorAttribute = attribute; //查找并设置(如果有)DefaultValueAttribute DefaultValueAttribute[] defaultValueAttribute = (DefaultValueAttribute[])propertyInfo.GetCustomAttributes(typeof(DefaultValueAttribute), true); if (defaultValueAttribute != null && defaultValueAttribute.Length > 0) { _iPropertyGirdCell.DefaultValueAttribute = defaultValueAttribute[0]; } //查找并设置(如果有)PropertyEditorAttribute if (editorAttribute != null && editorAttribute.Length > 0) { _iPropertyGirdCell.PropertyEditorAttribute = editorAttribute[0]; } } _iPropertyGirdCell.Owner = this._propertyGrid; _iPropertyGirdCell.OwnerRow = this; _propertyGridCell.Style.BackColor = Color.White; _propertyGridCell.Style.ForeColor = Color.Black; _propertyGridCell.Style.SelectionBackColor = Color.White; _propertyGridCell.Style.SelectionForeColor = Color.Black; this.Cells.Add(_propertyGridCell); #endregion DataGridViewCell dcProperty = new DataGridViewTextBoxCell(); //属性的真实名 dcProperty.Value = propertyInfo.Name; this.Cells.Add(dcProperty); #endregion //此处的只读设置是享有最高优先级的 this.ReadOnly = attribute.ReadOnly; //初始化子属性行 InitSubRow(); }