【问题标题】:How to add progress bar to Telerik RadPropertyGrid?如何向 Telerik RadPropertyGrid 添加进度条?
【发布时间】:2020-07-08 10:36:33
【问题描述】:

我使用 Telerik WinForms 包。 我的 RadPropertyGrid 显示一些设置:

radPropertyGrid1.SelectedObject = m_db;
radPropertyGrid1.ReadOnly = true;

如何将新项目添加为 ProgressBar 以显示百分比和颜色?它是静态字段,无需动态更改任何内容。

或者作为第二种方式:如何用 % 添加彩色字段?

【问题讨论】:

    标签: winforms telerik propertygrid


    【解决方案1】:

    为了在 RadPropertyGrid 中显示进度条,可能的解决方案是使用自定义项。 RadPropertyGrid 允许您创建和使用自己的自定义值项,允许您添加所需的编辑器以满足您的业务需求。您可以在此处找到有关此主题的更多信息:https://docs.telerik.com/devtools/winforms/controls/propertygrid/custom-items

    为了您的参考,我准备了一个示例演示如何实现您的要求。请注意,这是一个示例演示,可能无法涵盖所有​​可能的情况。随意根据您的特定需求对其进行修改或扩展。

    请参考以下代码sn-p:

    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
            PropertyStoreItem stringItem = new PropertyStoreItem(typeof(string), "String", "Telerik", "Property storing a string value", "Telerik");
            RadPropertyStore store = new RadPropertyStore();
            store.Add(stringItem);
            PropertyStoreItem progressItem = new PropertyStoreItem(typeof(int), "Progress", 40, "Represents a progress bar element.");
            store.Add(progressItem);
            this.radPropertyGrid1.SelectedObject = store;
    
            this.radPropertyGrid1.CreateItemElement += this.RadPropertyGrid1_CreateItemElement;
        }
    
        private void RadPropertyGrid1_CreateItemElement(object sender, CreatePropertyGridItemElementEventArgs e)
        {
            if (e.ItemElementType == typeof(PropertyGridItemElement))
            {
                if (e.Item.Name == "Progress")
                {
                    e.ItemElementType = typeof(CustomItemElement);
                }
                else
                {
                    e.ItemElementType = typeof(DefaultPropertyGridItemElement);
                }
            }
        }
    }
    
    
    public class CustomPropertyGridValueElement : PropertyGridValueElement
    {
        RadProgressBarElement progressBarElement;
        protected override void CreateChildElements()
        {
            base.CreateChildElements();
            progressBarElement = new RadProgressBarElement();
            this.Children.Add(progressBarElement);
        }
    
        public override void Synchronize()
        {
            PropertyGridItem item = this.VisualItem.Data as PropertyGridItem;
    
    
            if (item != null && item.Value != DBNull.Value)
            {
                this.progressBarElement.Value1 = Convert.ToInt16(item.Value);
            }
        }
    }
    
    public class CustomItemElement : PropertyGridItemElement
    {
        protected override PropertyGridValueElement CreatePropertyGridValueElement()
        {
            return new CustomPropertyGridValueElement();
        }
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(PropertyGridItemElement);
            }
        }
        public override bool IsCompatible(PropertyGridItemBase data, object context)
        {
            return data.Label == "Progress";
        }
    }
    public class DefaultPropertyGridItemElement : PropertyGridItemElement
    {
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(PropertyGridItemElement);
            }
        }
        public override bool IsCompatible(PropertyGridItemBase data, object context)
        {
            return data.Label != "Progress";
        }
    }
    

    enter image description here

    【讨论】:

    • Thanx Nadya,这是我想要实现的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    • 2012-04-17
    相关资源
    最近更新 更多