【问题标题】:WPF: How Do I edit the column properties of a base/parent class in the child class through the property editor/XAML?WPF:如何通过属性编辑器/XAML 编辑子类中基类/父类的列属性?
【发布时间】:2010-12-03 11:02:09
【问题描述】:

我创建了一个名为 ProductionDataUserControlBase 的类,它派生自 UserControl 类。此基类没有 XAML。它的目的是充当我封装在类中的网格的基类,以便以后继承该类时可以对其进行修改。在基类的构造函数中,我还创建了列并将它们添加到 Grid 的集合中。我创建了一个名为 Columns 的公共属性,它所做的只是返回(获取)网格的 Columns 属性集合。

我创建了一个派生自 ProductionDataUserControlBase 的子类,它确实包含 XAML。在继承控件的属性编辑器中,我的 Columns 集合存在。我可以通过属性编辑器打开集合并添加新列。但是,即使我可以直观地看到画布上的列,列编辑器也不包含我在基础中添加的列。

我假设这是因为我在基础中添加的列不在子项的 XAML 中。如果我通过子项的 XAML 添加列,它会创建重复的列,因为它们是在基础中添加的。如何在不使用子代码隐藏的情况下编辑添加到基础中的列的属性?

public partial class ProductionDataUserControlBase : UserControl
    {
        private RadGridView _grdProdData;
        private Boolean _InitCalled = false; //Boolean variable used to control whether init was previously called.
        //This is because the loaded event may be fired multiple times depending
        //on what type of control it's been placed into.

        public ProductionDataUserControlBase()
        {
            InitializeComponent();
        }        


        public Telerik.Windows.Controls.GridViewColumnCollection Columns
        {
            get
            {
                return _grdProdData.Columns;            
            }            
        }        

        protected virtual void InitializeComponent()
        {
            if (!_InitCalled)
            {                
                InitializeGrid();
                Columns = _grdProdData.Columns;
                this.AddChild(_grdProdData);                                                

                _InitCalled = true;
            }
        }

        private void InitializeGrid()
        {
            Telerik.Windows.Controls.GridViewColumn grdCol = null;

            this._grdProdData = new RadGridView();
            this._grdProdData.AutoGenerateColumns = false;

            grdCol = new Telerik.Windows.Controls.GridViewColumn() { HeaderText = "PSTAT", Name = "grdColPSTAT", UniqueName = "PSTAT" };
            _grdProdData.Columns.Add(grdCol);

            grdCol = new Telerik.Windows.Controls.GridViewColumn() { HeaderText = "PCONO", Name = "grdColPCONO", UniqueName = "PCONO" };
            _grdProdData.Columns.Add(grdCol);

         }

    }

<ProductionDataUserControlBase x:Class="AmerenProductionDataUserControl"    
            xmlns:MSControls="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns="clr-namespace:AmerenProductionDataUserControl;assembly=AmerenProductionDataUserControl" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">

    </ProductionDataUserControlBase>

public partial class AmerenProductionDataUserControl : ProductionDataUserControlBase
        {

            public AmerenProductionDataUserControl()
            {
                InitializeComponent();                                            
            }
        }

【问题讨论】:

    标签: wpf gridview properties user-controls collections


    【解决方案1】:

    听起来你要么覆盖了集合的内容,要么覆盖了属性。

    【讨论】:

    • 感谢您的回复,但也许您可以进一步解释。您的回复表明我覆盖了一个属性——我假设你的意思是网格——但在上面的代码中,它表明我只公开了 column 属性,因为我不是从网格继承而是从用户控件继承。因为我是从用户控件而不是网格继承的,所以不可能从网格中覆盖列集合。
    猜你喜欢
    • 2011-06-16
    • 1970-01-01
    • 2010-11-10
    • 2011-10-08
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多