2016-05-11 18 views
0

私はWPFを初めて使用しています。私は、最終プロジェクトを迅速に作成するために、いくつかのサンプルクラスを作成しています。私は基底クラスとしてCustomControl1を持ち、基底xamlとしてGenericを、そしてxamlとクラスとしてはwInheritを得ました。私のベースウィンドウのXAMLはここにある:ここでベースxamlでclickイベントを実装し、WPFの基底クラスでそれを処理します

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:Palayeshgah"> 
<!--x:Class="Palayeshgah.CustomControl1">--> 
<Window.Resources> 
    <Style x:Key="btnClose" TargetType="{x:Type Button}" > 
     <Setter Property="Template"> 
      ... 
     </Setter> 
    </Style> 
</Window.Resources> 

... 

<Grid> 
    <Button Style="{StaticResource btnClose}" Name="btnClose" Content="" HorizontalAlignment="Left" Margin="274,0,0,0" VerticalAlignment="Top" Width="18" Height="21"/> 
</Grid> 

は私の派生XAMLです:

public class CustomControl1 : Window 

そして、これは私の派生クラスである:ここでは

<ns:CustomControl1 x:Class="Palayeshgah.PL.Base.wInherit" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:ns="clr-namespace:Palayeshgah" 
    Title="Inheritance Sample" Height="300" Width="300" Background="Wheat" MouseEnter="CustomControl1_MouseEnter"/> 

は私の基本クラスです:

public partial class wInherit : CustomControl1 

これらのコードは基本設計を完全に示しています。しかし、私はボタンのクリックイベントを無効にする必要があります。ベースxamlのbtnCloseClick="btnClose_Click"を追加すると、クラスをintrtoduceにする必要があります。Generic.xaml。ただ、以下のように:

Missing partial modifier on declaration of type 'Palayeshgah.CustomControl1'; another partial declaration of this type exists

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:Palayeshgah" 
x:Class="Palayeshgah.CustomControl1"> 

それから私はとエラーが発生します。

私はCustomControl1partialを追加し、エラーが以下のように変更されました:

Partial declarations of 'Palayeshgah.CustomControl1' must not specify different base classes 

私はここでハングアップ。

私はbtnClose_Clickを忘れていませんでした。基本クラスのクリックイベントです:

protected virtual void btnClose_Click(object sender, RoutedEventArgs e) 
    { 
     Close(); 
    } 

基本となるxamlをベースクラスから分離する理由は何ですか?答えは、そのアプローチで基本設計を示すことができなかったことです。

WPFのウィンドウとイベントの継承方法を教えてください。

答えて

1

CustomControl1クラスを継承しないことをお勧めします。 Generic xamlのusercontrolを作成し、それを派生したxamlに含めます。

+0

'CustomControl1'と' Generic'の代わりに新しい 'UserControl'を追加し、' 'でそれを呼び出しました。 'BaseUserControl'は新しいベースユーザコントロールです。私はそれを新しい窓にドッキングし、それは魅力のように機能します。あなたの簡単な手がかりをありがとう。 –

関連する問題