2010-12-31 34 views
4

System.Windows.Windowから派生したカスタムウィンドウタイプを作成する際に問題が発生しているようです。発生している2つの問題があるようです。まず、述べコンパイル時エラーがありますWPF:ウィンドウから継承

タイプの静的メンバ 「ContentProperty」を見つけることができません 「コントロール」

これがためのControlTemplateでのContentPresenter要素への参照である

カスタムウィンドウ(下記のBaseWindowResource.xamlのコードサンプルを参照)。 BaseWindowがWindowから派生しているため、なぜこれが起きているのか分かりません。したがって、Contentプロパティが必要です。

2番目の問題は、BaseWindowのContentRenderedイベントを得ることができないという事実です。 BaseWindowから派生したWindow1がレンダリングを終了したときに起動します。BaseWindowでContentRenderedイベントを処理する必要があります。ハンドラには、派生クラスごとにコピーする必要があるコードが多数含まれているためです。

いずれにせよ、ここにコードがあります。どんな助けでも大歓迎です!

乾杯、

アンドリュー

App.xaml:

<Application x:Class="WpfApplication4.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      StartupUri="Window1.xaml"> 
    <Application.Resources> 
     <ResourceDictionary Source="/BaseWindowResource.xaml" /> 
    </Application.Resources> 
</Application> 

BaseWindowResource.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:WpfApplication4"> 
    <Style TargetType="{x:Type local:BaseWindow}" x:Key="BaseWindowStyleKey"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate> 
        <Grid> 
         <Rectangle Margin="20" Fill="Green" x:Name="MyRect" /> 
         <ContentPresenter Margin="30" x:Name="MyContentPresenter" 
              Content="{TemplateBinding Content}" 
              ContentTemplate="{TemplateBinding ContentTemplate}" /> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</ResourceDictionary> 

BaseWindow.cs:

public class BaseWindow : Window 
    { 
     public BaseWindow() 
     { 
      Style = FindResource("BaseWindowStyleKey") as Style; 

      ContentRendered += new EventHandler(BaseWindow_ContentRendered); 
     } 

     void BaseWindow_ContentRendered(object sender, EventArgs e) 
     { 
      ContentPresenter contentPresenter = Template.FindName("MyContentPresenter", this) as ContentPresenter; 

      MessageBox.Show(String.Format("The dimensions for the content presenter are {0} by {1}", 
       contentPresenter.ActualWidth, 
       contentPresenter.ActualHeight)); 
     } 
    } 

Window1.xaml:

<local:BaseWindow x:Class="WpfApplication4.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:WpfApplication4" 
        Title="Window1" Height="300" Width="300"> 
</local:BaseWindow> 

し、最終的にWindow1.xaml.cs:

public partial class Window1 : BaseWindow 
{ 
    public Window1() 
    { 
     InitializeComponent(); 
    } 
} 

まあ、それはすべてのコードです。それは問題をかなり隔離します。

乾杯、

アンドリュー

答えて

8

は次のようなタイプを指定してみてください。

Content="{TemplateBinding Window.Content}" 

私は第二の問題は、最初の1つに関する思います。この解決法で秒が解決されない場合は、ここにコメントを投稿してください。

+0

うわーを解決する必要があります追加するには、プロパティ名を修飾について正しかったです。しかし、ContentRenderedイベントはまだ発火していません... – Andrew

+0

コンテンツがありますか?次のMSDNの状態: 'ウィンドウにコンテンツがない場合、このイベントは発生しません。 http://msdn.microsoft.com/en-us/library/system.windows.window.contentrendered.aspx – decyclone

+0

Decycloneに感謝し、問題を修正しました。良い目。私はそのページを読んでいることを知っていますが、私は十分注意を払っていないと思います。ありがとう! – Andrew

1

TargetTypeが問題に

exemple

<ControlTemplate TargetType="Window">