WPF PropertyGrid 问题

发布于 2024-09-30 14:18:30 字数 364 浏览 3 评论 0原文

我正在尝试创建一个基于 WPF 的 PropertyGrid。

最近我尝试了 wpg.codeplex.com 项目,但是我在使用这个控件时遇到了一些问题。 现在,我正在尝试开发 http://blog.joachim.at/?p=36 这个项目。

我成功添加了枚举值和支持,但我在集合方面遇到了问题。

例如,我的自定义对象有一个名称为 City 且类型为 Guid 的属性。 我希望,用户可以从组合框中选择城市。

我正在与 TypeConverts、IValueConverts 等进行斗争。

我该如何解决这个问题?

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 want, users can select City from combobox.

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

How can i solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音栖息无 2024-10-07 14:18:30

经过几个小时的工作,我解决了我的问题。
我只需要 TypeConverter 来解决这个问题,所以我创建了一个派生自 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);
            }
        }

使用自定义类型转换器

class MyClass
{

// my some props..

// my some props..

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

// my some methods..
}

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..
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文