开发者

WPF PropertyGrid Problem

I am trying to create a WPF based PropertyGrid.

Recently i tried wpg.codeplex.com project, but i had some problems with this control. Now, i am trying to develop http://blog.joachim.at/?p=36 this project.

I successfully added Enum Values, support but i have got problems with collections.

For example my custom object has a property that name is City and type is Guid. I wan开发者_开发百科t, users can select City from combobox.

I was fighting with TypeConverts, IValueConverts, and etc..

How can i solve this?


After hours of work i solved my problem. I had need only TypeConverter to solve this, so i created a class that derives from TypeConverter.

    class PierListConverter : TypeConverter
        {
            ArrayList piers = new ArrayList();
            public PierListConverter()
            {

            }
            public override bool
            GetStandardValuesSupported(ITypeDescriptorContext context)
            {
                return true;
            }
            public override StandardValuesCollection
            GetStandardValues(ITypeDescriptorContext context)
            {
                // This method returns me the list that will use to fill combo at property grid.
                piers.Clear();
                foreach (var item in GullsEyeModel.GetInstance().GetPiers())
                {
                    piers.Add(item.Id);
                }
                StandardValuesCollection cols = new  StandardValuesCollection(piers);
                return cols;
            }
            public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
            {
               // If this method returns true, ConvertFrom method will invoke
                if (sourceType == typeof(string))
                {
                    return true;
                }
                else
                return base.CanConvertFrom(context, sourceType);
            }
            public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
            {
                // In this method i am getting selected text and querying; after that i retrieve proparete Guid value and then returning back to my object that binded property grid.
                if (value != null)
                {
                    if (value.ToString() == "Seçiniz")
                    {
                        return Guid.Empty;
                    }
                    else if (!string.IsNullOrEmpty(value.ToString()))
                    {
                        GuidConverter g = new GuidConverter();
                        PierItem[] pierArray = GullsEyeModel.GetInstance().GetPiers();
                        PierItem selectedPier = pierArray.Where(item => item.Info.Name == value.ToString()).FirstOrDefault();
                        if (selectedPier != null)
                        {
                            return selectedPier.Id;
                        }
                        else
                            return base.ConvertFrom(context, culture, value);
                    }
                    else
                        return base.ConvertFrom(context, culture, value);
                }
                else
                return base.ConvertFrom(context, culture, value);
            }
            public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
            {
// In this method i am converting ID to string (Name) to display in Property Grid
                if (value != null)
                {
                    GuidConverter g = new GuidConverter();
                    PierItem[] piers = GullsEyeModel.GetInstance().GetPiers();
                    PierItem selectedPier = piers.Where(item => item.Id== (Guid)g.ConvertFromString(value.ToString())).FirstOrDefault();
                    if (selectedPier != null)
                    {
                        return selectedPier.Info.Name;
                    }
                    else
                        return "Seçiniz";
                }
                else
                return base.ConvertTo(context, culture, value, destinationType);
            }
        }

Using custom TypeConverter

class MyClass
{

// my some props..

// my some props..

[TypeConverter(typeof(PierListConverter))]      
public Guid PierId {get; set;}

// my some methods..
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜