2009-05-20 221 views
80

以下WPF用戶控件名爲DataTypeWholeNumber它的工作原理。WPF UserControl如何繼承WPF UserControl?

現在我想打一個用戶控件稱爲DataTypeDateTimeDataTypeEmail

許多依賴屬性將所有這些控件可以共享,所以我希望把自己的常見的方法進入BaseDataType並且每個這些UserControls都從此基類型繼承。

然而,當我這樣做,我得到錯誤:部分聲明可能不會有不同的基類

那麼我如何實現UserControl的繼承,所以共享的功能都在基類中?

using System.Windows; 
using System.Windows.Controls; 

namespace TestDependencyProperty827.DataTypes 
{ 
    public partial class DataTypeWholeNumber : BaseDataType 
    { 
     public DataTypeWholeNumber() 
     { 
      InitializeComponent(); 
      DataContext = this; 

      //defaults 
      TheWidth = 200; 
     } 

     public string TheLabel 
     { 
      get 
      { 
       return (string)GetValue(TheLabelProperty); 
      } 
      set 
      { 
       SetValue(TheLabelProperty, value); 
      } 
     } 

     public static readonly DependencyProperty TheLabelProperty = 
      DependencyProperty.Register("TheLabel", typeof(string), typeof(BaseDataType), 
      new FrameworkPropertyMetadata()); 


     public string TheContent 
     { 
      get 
      { 
       return (string)GetValue(TheContentProperty); 
      } 
      set 
      { 
       SetValue(TheContentProperty, value); 
      } 
     } 

     public static readonly DependencyProperty TheContentProperty = 
      DependencyProperty.Register("TheContent", typeof(string), typeof(BaseDataType), 
      new FrameworkPropertyMetadata()); 


     public int TheWidth 
     { 
      get 
      { 
       return (int)GetValue(TheWidthProperty); 
      } 
      set 
      { 
       SetValue(TheWidthProperty, value); 
      } 
     } 

     public static readonly DependencyProperty TheWidthProperty = 
      DependencyProperty.Register("TheWidth", typeof(int), typeof(DataTypeWholeNumber), 
      new FrameworkPropertyMetadata()); 



    } 
} 
+0

有關WPF解決方法的可視化繼承,請參閱:http://svetoslavsavov.blogspot.gr/2009/09/user-control-inheritance-in-wpf.html或用於顯式定義祖先中的GUI,請參閱http:// support.microsoft.com/kb/957231 – 2015-01-10 01:44:17

回答

114

確保你已經改變了第一標記在XAML也從你的新基本類型繼承

所以

<UserControl x:Class="TestDependencyProperty827.DataTypes.DataTypeWholeNumber" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
    xmlns:s="clr-namespace:System;assembly=mscorlib" 
    > 

成爲

<myTypes:BaseDataType x:Class="TestDependencyProperty827.DataTypes.DataTypeWholeNumber" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
    xmlns:s="clr-namespace:System;assembly=mscorlib" 
    xmlns:myTypes="clr-namespace:TestDependencyProperty827.DataTypes" 
    > 

所以,總結完整的答案包括以下評論的額外細節:

  • 基類不應包含xaml文件。將其定義在單個(非部分)cs文件中,並將其定義爲直接從Usercontrol繼承。
  • 確保子類在cs代碼隱藏文件和xaml的第一個標記(如上所示)中都從基類繼承。
+3

當我做出更改時,出現錯誤:「... DataTypes.BaseDataType不能是XAML文件的根,因爲它是使用XAML定義的」,您如何擺脫這種困境循環參考? – 2009-05-20 12:16:57

+9

如果你可以讓基類成爲一個從UserControl繼承而沒有定義任何xmal的正常類型(所以不需要對兩個文件進行部分類拆分)。問題是你不能繼承xmal,所以基類或子類都不需要xmal文件。要麼或者讓你的類自定義控件而不是用戶控件。這樣外觀就是在部分類之外定義的,但是您將失去用戶控件的易用性。 – 2009-05-20 12:24:00

1

我發現本文中的答案:http://www.paulstovell.com/xmlnsdefinition

基本上是什麼說的是,你應該定義在AssemlyInfo.cs文件,它可以將在XAML中使用的XML命名空間。它爲我工作,但我把基本的用戶控制類放在一個單獨的DLL ...

0

我遇到了同樣的問題,但需要有控制從抽象類繼承,這是設計器不支持。什麼解決了我的問題是使usercontrol繼承標準類(繼承UserControl)和接口。這樣設計師正在工作。

//the xaml 
<local:EcranFiche x:Class="VLEva.SIFEval.Ecrans.UC_BatimentAgricole" 
        xmlns:local="clr-namespace:VLEva.SIFEval.Ecrans" 
        ...> 
    ... 
</local:EcranFiche> 

// the usercontrol code behind 
public partial class UC_BatimentAgricole : EcranFiche, IEcranFiche 
{ 
    ... 
} 

// the interface 
public interface IEcranFiche 
{ 
    ... 
} 

// base class containing common implemented methods 
public class EcranFiche : UserControl 
{ 
    ... (ex: common interface implementation) 
} 
0
public partial class MooringConfigurator : MooringLineConfigurator 
    { 
     public MooringConfigurator() 
     { 
      InitializeComponent(); 
     } 
    } 



<dst:MooringLineConfigurator x:Class="Wave.Dashboards.Instruments.ConfiguratorViews.DST.MooringConfigurator" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:dst="clr-namespace:Wave.Dashboards.Instruments.ConfiguratorViews.DST" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="400"> 

    <Grid x:Name="LayoutRoot" Background="White"> 

    </Grid> 
</dst:MooringLineConfigurator>  
0

有由設計師創建的部分類定義,您可以通過InitializeComponent()方法的定義打開它簡單的方法。 然後,只需將UserControl的部分類繼承關係更改爲BaseDataType(或您在類定義中指定的任何類)。

之後,您將有警告,說明InitializeComponent()方法在子類中隱藏。

因此,您可以將CustomControl作爲base clas而不是UserControl以避免基類中的部分定義(如一個註釋中所述)。