PropertyGrid使用总结

研究了一上午PropertyGrid遇到了许多问题,感觉一时用不上,暂时先用TextBlock与TextBox拼凑吧。
总结一下:
一、下载地址:http://slg30.codeplex.com/
直接Download就得到一个项目。在vs08下打开看看。
二、移植到自己的项目中,我把Assets、Converters、Themes三个目录与所有的.cs文件全部复制到自己的Silverlight项目中。
使用的时候,注意前台引用:xmlns:slg="clr-namespace:SL30PropertyGrid"
后台引用:using SL30PropertyGrid;
照着例子测试一下,挺好用。

三、做个自己的东西。
在BusinessObject.cs中添加代码:

大气象
public class BaseInfo : INotifyPropertyChanged
    {
        
private string _roomName;
        
private int _sameRoomNum;
        
private TimeSet _TimeSet;

        [Category(
"基本信息")]
        
public string 房间名称
        {
            
get { return _roomName; }
            
set
            {
                
if (_roomName == value) return;
                _roomName 
= value;
                OnPropertyChanged(
"roomName");
            }
        }
        [Category(
"基本信息")]
        
public int 同层相同房间个数
        {
            
get { return _sameRoomNum; }
            
set
            {
                
if (_sameRoomNum == value) return;
                _sameRoomNum 
= value;
                OnPropertyChanged(
"同层相同房间个数");
            }
        }
        [Category(
"基本信息")]
        
public TimeSet 时间指派
        {
            
get { return _TimeSet; }
            
set
            {
                
if (_TimeSet == value) return;
                _TimeSet 
= value;
                OnPropertyChanged(
"时间指派");
            }
        }

        
public enum TimeSet
        {
            人员_办公建筑_工作日,
            人员_办公建筑_节假日
        }
        
        
#region INotifyPropertyChanged Members

        
public event PropertyChangedEventHandler PropertyChanged;

        
private void OnPropertyChanged(string propertyName)
        {
            
if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException("propertyName");
            PropertyChangedEventHandler handler 
= PropertyChanged;
            
if (handler != null) handler(thisnew PropertyChangedEventArgs(propertyName));
        }

        
#endregion
    }

 

四、遇到两个问题,一个是如何转换颜色。
类似"#EEFFCC"怎样转换成Color.FromArgb(255, 94, 170, 255);
已经提问:http://space.cnblogs.com/question/15170/
另外是关于排序,默认是按照拼音排序。我希望能按照属性书写的先后次序排序。没能实现。
参考:在c#的propertyGrid中,自定义对属性的排序
//
        // 摘要:
        //     使用该集合的默认排序(通常为字母顺序)对集合中的成员进行排序。
        public virtual PropertyDescriptorCollection Sort();
        //
        // 摘要:
        //     使用指定的 System.Collections.IComparer 对此集合中的成员排序。
        public virtual PropertyDescriptorCollection Sort(IComparer comparer);
        //
        // 摘要:
        //     对此集合中的成员排序。首先应用指定的顺序,然后应用此集合的默认排序,后者通常为字母顺序。
        public virtual PropertyDescriptorCollection Sort(string[] names);
        //
        // 摘要:
        //     对此集合中的成员排序。首先应用指定的顺序,然后使用指定的 System.Collections.IComparer 进行排序。
        public virtual PropertyDescriptorCollection Sort(string[] names, IComparer comparer);

        /// <summary>
        /// 返回此类型说明符所表示的对象的已筛选属性描述符的集合。
        /// </summary>
        /// <param name="attributes">用作筛选器的属性数组。它可以是 null。</param>
        /// <returns>
        /// 一个 <see cref="T:System.ComponentModel.PropertyDescriptorCollection"></see>,包含此类型说明符所表示的对象的属性说明。默认为 <see cref="F:System.ComponentModel.PropertyDescriptorCollection.Empty"></see>。
        /// </returns>
         public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            int i = 0;
            //parameterList是外面传入的待显示数据。
            PropertyDescriptor[] newProps = new PropertyDescriptor[parameterList.Count];
            foreach (ConfigParameter parameter in parameterList)
            {
                //由ConfigParameter,你自己定义的类型,转成PropertyDescriptor类型:
                newProps[i++] = new SystemOptionsPropertyDescriptor(parameter, true, attributes);
            }

            //取得了PropertyDescriptor[] newProps;现在可以对它排序,否则就按照newProps的原有顺序显示;
           
            //1.直接返回

            PropertyDescriptorCollection tmpPDC = new PropertyDescriptorCollection(newProps);
            return tmpPDC;

            //2.默认字母顺序
            PropertyDescriptorCollection tmpPDC = new PropertyDescriptorCollection(newProps);
            return tmpPDC.Sort();

            //3.数组的顺序:sortName就是属性的顺序,内容就是各个属性名。
            string[] sortName = new string[] { "a","b","c","d" };
            return tmpPDC.Sort(sortName);

            //4.comparer规则定义的排序方式
            ParameterListComparer myComp = new ParameterListComparer();//ParameterListComparer : IComparer
            return tmpPDC.Sort(myComp);

            //5.先数组,后comparer
            return tmpPDC.Sort(sortName,myComp);

            //而我采用的是把数据传入之前就排序完毕的方法:即调用之前,把数据parameterList排好顺序,再 1.直接返回。
        }

        //对于数组排序方法,可以声明称一个事件,由外部传入属性的实现顺序:
        public delegate string[] SortEventHandler();
        public class CustomTypeDescriptorExtend : CustomTypeDescriptor
        {
            public SortEventHandler OnSort;

            //......其它代码

            public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
            {
                string[] sortName = OnSort();
                PropertyDescriptorCollection tmpPDC = TypeDescriptor.GetProperties(typeof(CustomTypeDescriptorExtend), attributes);
                return tmpPDC.Sort(sortName);
            }
        }

        //使用时:
        public MyMethod()
        {
            CustomTypeDescriptorExtend s = new CustomTypeDescriptorExtend();
            s.OnSort += new SortEventHandler(SetSortors);
            PropertyGrid1.SelectedObject = s;
            PropertyGrid1.PropertySort = PropertySort.Categorized;
        }

        public string[] SetSortors()
        {
            return new string[] { "B", "A", "C" };
        }

 

posted @ 2010-06-21 12:57  大气象  阅读(6330)  评论(3编辑  收藏  举报
http://www.tianqiweiqi.com