【问题标题】:WPF - UserControl inheritanceWPF - 用户控件继承
【发布时间】:2010-10-20 20:50:59
【问题描述】:

我遇到了 WPF 中的控件继承问题。我创建了一个名为 BaseUserControl 的用户控件。我希望这个控件成为其他 WPF userControls 的基本控件。所以我写了另一个名为 FirstComponent 的 UserControl。在下一步中,我更改了此代码

FirstComponent : UserControl

到这里

FirstComponent : BaseControl

但是在编译过程中我得到了这个错误

Partial declarations of 'controlinheritance.componenets.FirstComponent' must not specify different base classes 

我应该怎么做才能让 FirstComponent 从 BaseControl 派生?

编辑 感谢 abhishek 的回答,我设法继承了 controls 。但是我还有一个问题。在基类中,我指定了一个属性 public Grid _MainGrid { get;放; }。现在我想在我的派生类中创建这个网格的一个实例。所以我使用了这段代码 但是我得到一个错误属性'_MainGrid'没有值。第 8 行位置 36。

【问题讨论】:

    标签: wpf inheritance user-controls wpf-controls


    【解决方案1】:

    你看到我的完整文章了吗?

    http://www.dotnetfunda.com/articles/article832-define-base-class-for-window--usercontrol-.aspx

    我希望这对您有所帮助。

    如果你尝试执行项目,它肯定会抛出错误 你。这是因为,每个 WPF 窗口都是从 baseWindow 创建的 布局而不是当前的窗口布局。换句话说,如果你 看 XAML,你会看到根标签是 Window,它是一个类 只是当前窗口的父级。

    因此,为了确保一切正常,我们需要更改 Root 元素。

    所以它看起来像:

    <local:BaseWindow Class="BaseWindowSample.Window1" 
                      Name="winImp" 
                      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                      x="http://schemas.microsoft.com/winfx/2006/xaml" 
                      xmlns:local="clr-namespace:BaseWindowSample" 
                      Title="Window1">
    ...
    </local:BaseWindow>
    

    如果您仔细查看,您会发现我已将一个命名空间添加到我的 项目并将其命名为本地。所以 BaseWindow 应该来自 BaseWindow,因此它就像 local:BaseWindow

    【讨论】:

    • 感谢它对我帮助很大。你能看看我编辑的消息吗
    【解决方案2】:

    最初错误的原因是因为该类实际上是一个部分类,除了您更改基类的位置之外,它还在其他地方列出了特定的基继承。

    至于你的财产“继承”,我建议尝试

    public Grid MainGrid 
    { 
       get 
       { 
          return base.MainGrid; 
       } 
    
       set 
       { 
          base.MainGrid = value; 
       } 
    }
    

    但是我应该注意,这不会为您提供指向基类的任何现有实例的链接。如果您希望派生类中有保证链接到该 Grid 的唯一实例,那么您必须将基类属性设为静态。 在这种情况下,您的代码将如下所示...

    public Grid MainGrid
    {
        get
        {
            return BaseControl.MainGrid;
        }
    
        set
        {
            BaseControl.MainGrid = value;
        }
    }
    

    【讨论】:

      【解决方案3】:

      当您在 XAML.cs 文件中为用户控件指定不同的基类时

       FirstComponent : BaseControl
      

      您还应该在 XAML 中更改它

       <Base:BaseControl x:Class="FirstComponent"
                   xmlns:Base="clr-namespace:MyApplication.Base"
                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                   mc:Ignorable="d" 
                   d:DesignHeight="300" d:DesignWidth="300">
          <Grid>
      
      
          </Grid>
      </Base:BaseControl>
      

      【讨论】:

        猜你喜欢
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-10
        • 1970-01-01
        • 2013-09-03
        • 2017-11-14
        相关资源
        最近更新 更多