代码之家  ›  专栏  ›  技术社区  ›  user134363

WPF:如何通过property editor/XAML编辑子类中基类/父类的列属性?

  •  2
  • user134363  · 技术社区  · 17 年前

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

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

    我假设这是因为我在基中添加的列不在子级的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();                                            
                }
            }
    
    1 回复  |  直到 17 年前
        1
  •  0
  •   Danny Varod    17 年前

    听起来您要么重写集合的内容,要么重写属性。

    推荐文章