2012-04-22 8 views
0
<ContentControl x:Class="Test.MyControl" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      Width="200" Height="200" > 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 
     <Rectangle Fill="Blue"/> 
     <ContentPresenter Grid.Row="1" Content="{TemplateBinding ContentControl.Content}" /> 
     <Rectangle Fill="Yellow" Grid.Row="2"/> 
    </Grid> 
</ContentControl> 

<Window x:Class="Test.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:Test="clr-namespace:Test" Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Test:MyControl2> 
      <Button/> 
     </Test:MyControl2> 
    </Grid> 
</Window> 

ボタンは、青色と黄色の四角形の間に表示されます。私のボタンがContentPresenter領域に表示されないのはなぜですか?

私は間違っていますか?

+0

私はあなたがなぜあなたはコントロール内のボタンを配置していけない、ユーザーコントロール内にこのようにボタンを置くことができると思ういけませんか? – Habib

答えて

3

問題は、ContentControlのコンテンツを2回、ContentControlで1回、Window.xamlで2回定義することです。 Window.xamlの内容はContentControlの内容よりも優先されるため、その上と下に色の付いた四角形のないボタンが表示されます。

ContentControlのコンテンツの表示方法を変更する場合は、ContentControlのContentTemplateに関連するマークアップを配置する必要があります。あなたは上記のContentControlには、次のようなものを探す必要があります:

<ContentControl x:Class="Test.MyControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Width="200" Height="200" > 
    <ContentControl.ContentTemplate> 
     <DataTemplate> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="*" /> 
        <RowDefinition Height="*" /> 
        <RowDefinition Height="*" /> 
       </Grid.RowDefinitions> 
       <Rectangle Fill="Blue"/> 
       <ContentPresenter Grid.Row="1" Content="{TemplateBinding ContentControl.Content}" /> 
       <Rectangle Fill="Yellow" Grid.Row="2"/> 
      </Grid> 
     </DataTemplate> 
    </ContentControl.ContentTemplate> 
</ContentControl> 
-1

は、私はプロではないんだけど、私はこれらの行を変更します

<Rectangle Fill="Blue"/> 
    <ContentPresenter Grid.Row="1" Content="{TemplateBinding ContentControl.Content}" /> 
    <Rectangle Fill="Yellow" Grid.Row="2"/> 

この目的のために:

<Rectangle Fill="Blue" Grid.Row="0"/> 
    <ContentPresenter Grid.Row="1" Content="{TemplateBinding ContentControl.Content}" /> 
    <Rectangle Fill="Yellow" Grid.Row="2"/> 

ショート:あなたは、最初の1の行を定義するために忘れてしまいました。

+0

デフォルトの行と列は0(最初の行)です。 –

関連する問題