【发布时间】: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