2013-06-27 1 views
1

분명히 여기에 아주 기본적인 것이 없습니다. 나는 비슷한 질문을 찾았지만, 나는 내가 잘못하고있는 것을 이해할 수있는 답을 얻지 못했다.WPF 컨트롤에서 DataTemplate과 ControlTemplate을 모두 설정하면 작동하지 않습니다.

ControlTemplate을 설정하면 DataTemplate이 선택되지 않습니다.

내 문제에 대한 매우 간단한 예제를 만들었습니다

: 내가 뭘 원하는

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

    <Window.Resources> 
     <DataTemplate DataType="{x:Type local:Person}"> 
      <StackPanel> 
       <TextBlock Text="{Binding FirstName}" /> 
       <TextBlock Text="{Binding LastName}" /> 
      </StackPanel> 
     </DataTemplate> 
    </Window.Resources> 

    <ContentControl Content="{Binding Content}"> 
     <ContentControl.Template> 
      <ControlTemplate> 
       <Border BorderBrush="Blue" BorderThickness="2"> 
        <ContentPresenter /> 
       </Border> 
      </ControlTemplate> 
     </ContentControl.Template> 
    </ContentControl> 
</Window> 

런타임에로드를 선택 사용자 정의 데이터 템플릿은뿐만 아니라 내가, 예를 들어, 매 포장 할 요소에 상관없이 사용자 템플릿이 무엇이든 또는 템플릿을 전혀 지정하지 않은 경우에도 마찬가지입니다.

비슷한 질문에서 읽은 것부터 ControlTemplate에 <ContentPresenter />을 사용해야하지만 그 결과는 제거한 것과 같습니다. 테두리 만 표시됩니다.

+0

당신이 코드 숨김 보여줄 수 : 문제가 ControlTemplateTargetType 속성에 보인다? Window의 데이터 컨텍스트는 무엇입니까? – Bas

+0

코드가 없습니다. DataContext는'new {Content = any_object_possible} '입니다. – Markos

답변

3

귀하의 신청서를 재 작성했습니다.

<ContentControl Content="{Binding}"> 
    <ContentControl.Template> 
     <ControlTemplate TargetType="ContentControl"> 
      <Border BorderBrush="Blue" BorderThickness="2"> 
       <ContentPresenter /> 
      </Border> 
     </ControlTemplate> 
    </ContentControl.Template> 
</ContentControl> 
+0

아, 그게 다예요? 비슷한 질문에 대한 해결책을 찾지 못했습니다. 아무도 그처럼 어리석은 질문을하지 않습니다. 고맙습니다 :-) – Markos

+0

속성을 설정해야한다는 것은 분명하지 않습니다;) – Bas

관련 문제