2012-05-22 206 views
1

我正嘗試創建一個包含網格的Button,其中包含特定文本的行。ControlTemplate not showing Grid

Button將有兩行,兩行都有不同的文字。傳遞給ControlTemplate的文本正在顯示,但未顯示在模板中指定的文本。

此外,網格行的高度未顯示。我希望它能夠擴大按鈕的高度。事實上,我不確定網格是否真的顯示。

<Window x:Class="MAQButtonTest.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="695" Width="996">   
    <Window.Resources> 
     <Style TargetType="TextBlock"> 
      <Setter Property="OverridesDefaultStyle" Value="True"/> 
     </Style> 
     <ControlTemplate TargetType="Control" x:Key="1"> 
      <Grid Width="444"> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="51" /> 
        <RowDefinition Height="36" /> 
       </Grid.RowDefinitions> 
       <Grid Grid.Row="0" Background="#286c97"> 
        <TextBlock>This is the first piece of text</TextBlock> 
       </Grid> 
       <Grid Grid.Row="1" Background="#5898c0"> 
        <ContentPresenter Grid.Row="0" /> 
       </Grid> 
      </Grid> 
     </ControlTemplate> 
    </Window.Resources> 
    <Grid> 

     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="300" /> 
      <ColumnDefinition /> 
     </Grid.ColumnDefinitions> 
     <Grid Grid.Column="0" Background="#e9f1f6"></Grid> 
     <Grid Grid.Column="1" Background="#d2e3ed"> 
      <StackPanel> 
       <TextBlock FontFamily="Segoe UI" FontSize="22" FontWeight="Medium" Margin="52,58,0,0" Foreground="#0c3d5d">Your Quizzes <TextBlock FontFamily="Segoe UI" FontSize="18" FontWeight="Medium" Foreground="#0c3d5d">(7)</TextBlock></TextBlock> 
       <Grid> 
        <Button Width="444" Background="{x:Null}" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"> 
         <TextBlock>This is a second piece of text</TextBlock> 
        </Button> 
       </Grid> 
      </StackPanel> 
     </Grid> 

    </Grid> 
</Window> 

下面是它的顯示的時刻與我想要達到的按鍵佈局粗略說明:

Current vs What I want

回答

3

你設定按鈕的樣式,當您應該設置模板

資源:

<ControlTemplate TargetType="Button" x:Key="ButtonTemplate"> 
     <Grid Width="444"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="51" /> 
       <RowDefinition Height="36" /> 
      </Grid.RowDefinitions> 
      <Grid Grid.Row="0" Background="#286c97"> 
       <TextBlock>This is the first piece of text</TextBlock> 
      </Grid> 
      <Grid Grid.Row="1" Background="#5898c0"> 
       <ContentPresenter /> 
      </Grid> 
     </Grid> 
    </ControlTemplate> 

而且按鈕:

<Button Template="{StaticResource ButtonTemplate}" > 
     Text 2 
    </Button> 
0

你爲什麼要使用的TargetType爲「控制」,使用按鈕。您需要定義一個按鈕。使用混合來編輯按鈕模板以從中刪除所有不必要的內容。

+0

哎呦,我沒有意識到我仍然在那裏。我已將其更改爲按鈕,並且仍以完全相同的方式顯示:/ – Luke