2008-11-06 94 views
29

我對WPF相當陌生,我遇到了從用戶控件繼承的問題。從WPF中的用戶控件繼承

我創建了一個用戶控件,現在我需要繼承該控件並添加一些功能。

有沒有人做過這種事情?任何幫助將不勝感激。

謝謝

+2

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

回答

26

嗯..你創建基本控制

public abstract class BaseUserControl : UserControl{...} 

然後在XAML文件:

<Controls:BaseUserControl x:Class="Termo.Win.Controls.ChildControl" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:Controls="clr-namespace:Namespace.Of.Your.BaseControl"> 

這應該工作。

編輯:嗯..這個例子是有用的,當你有一個沒有XAML的基礎控制,然後從它繼承。另一種方式(從與Xaml的基本控制) - 我不知道你怎麼可以去做。

編輯2:顯然從this post + comments我把你想要的可能是不可能的。

+1

此解決方案適用於我。 – GeekyMonkey 2009-01-14 14:54:45

4

我可能有一點的解決方案:組合物,而不是繼承 - 我想出了控制,具有「內容槽」通過數據綁定從外部賦值,看看我的SO thread。使用的

實施例:

<UserControl ... > 

    <!-- My wrapping XAML --> 
     <Common:DialogControl> 
       <Common:DialogControl.Heading> 
         <!-- Slot for a string --> 
       </Common:DialogControl.Heading> 
       <Common:DialogControl.Control> 
         <!-- Concrete dialog's content goes here --> 
       </Common:DialogControl.Control> 
       <Common:DialogControl.Buttons> 
         <!-- Concrete dialog's buttons go here --> 
       </Common:DialogControl.Buttons> 
     </Common:DialogControl> 
    <!-- /My wrapping XAML --> 

</UserControl> 

在代碼隱藏一些處理代碼一起這將是對話窗口一個很好的基礎組分。

2

您可以通過使用delegate來完成此操作。

本質上,您需要創建一個接口(YourInterface),它包裝了所需的功能,然後使用戶控件和類都實現該接口。

接下來,請確保用戶控件引用了IYourInterface類型的對象,以便當您的用戶控件嘗試調用Interface的方法時,它會調用您的類的方法。

因爲用戶控件和類都實現相同的接口,所以它們可以看作是同一種類型的對象 - 這意味着您可以將它們都放入類型爲IYourInterface的對象集合中。這應該給你你想要的行爲。

在ASP中。NET我經常使用這種技術,通過讓我的類繼承abstract class祖先。我不明白爲什麼WPF不支持這一點。 :(

1

不能繼承的XAML代碼它的自我然而創建一個抽象類的代碼隱藏的,可以讓你在代碼編輯的背後,從一個派生類對象

XAML代碼:{Window1.xaml }

<Window 
x:Class="WPFSamples.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Height="auto" Width="256" Title="WPF Sameples"> 
    <Grid> 
    <Button x:Name="Button1" VerticalAlignment="Center" HorizontalAlignment="Center" Content="Click Me"/> 
    </Grid> 
</Window> 

代碼隱藏:{Window1.xaml.cs}

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace WPFSamples 
{ 
    /// <summary> 
    /// Interaction logic for Window1.xaml 
    /// </summary> 
    public abstract partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 
     } 
    } 
} 

派生類:{DisabledButtonWindow.cs}

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace WPFSamples 
{ 
    public sealed class DisabledButtonWindow : Window1 
    { 
     public DisabledButtonWindow() 
     { 
      Button1.IsEnabled = false; 
     } 
    } 
} 

儘管無法從wpf源自身繼承,但您可以使用此「Window1」控件作爲所有其他派生控件的模板。

1

我認爲你可以做到這一點,但你必須重新定義你在子類中的xaml中引用的任何函數和其他可能的東西。

IE如果你有一個你在基類xaml中訂閱的按鈕單擊事件,你將需要重寫子類中的按鈕單擊並調用基類按鈕單擊事件。

由於這是我正在繪製的同事代碼,所以沒有百分之百的確定細節,但認爲這將給任何希望在未來實現此目標的人帶來開始。