2010-12-31 7 views
4

System.Windows.Window에서 파생 된 사용자 지정 창 형식을 만드는 데 문제가있는 것 같습니다. 발생하는 두 가지 문제가있는 것 같습니다. 첫째, 내용의 컴파일 타임 오류가있다WPF : Window에서 상속 받기

유형의 정적 멤버 'ContentProperty'를 찾을 수 없습니다 '제어'

이는의 된 ControlTemplate에 ContentPresenter에 요소를 참조에

사용자 지정 창 (아래 BaseWindowResource.xaml의 코드 샘플 참조). BaseWindow가 Window에서 파생 된 이후로 왜 이런 일이 발생하는지 알지 못하므로 Content 속성이 있어야합니다 ...

두 번째 문제는 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)); 
     } 
    } 
,451,515,

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}" 

나는 두 번째 문제는 첫 번째에 관한 생각합니다. 초안이이 솔루션으로 해결되지 않으면 여기에 의견을 게시하십시오.

+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">