2012-05-22 48 views
1

我只是第一次嘗試使用ControlTemplate作爲我想要創建的按鈕。ControlTemplate導致錯誤「屬性'內容'被設置多次」

但是,只要我把標籤<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">   
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="300" /> 
      <ColumnDefinition /> 
     </Grid.ColumnDefinitions> 
     <Grid Grid.Column="0" Background="#FFE9F1F6"></Grid> 
     <Grid Grid.Column="1" Background="#FFD2E3ED"> 

     </Grid> 
    </Grid> 
    <ControlTemplate></ControlTemplate> 
</Window> 

我在哪裏放置標籤以防止出現此錯誤?

回答

1

模板與樣式,畫筆,DataTemplates一樣都是資源,通常放置在控件的資源字典或資源部分中。

<Window> 
    <Window.Resources> 
     <ControlTemplate TargetType="{x:Type Button}"/> 
     <ControlTemplate x:Key="myTemplate" TargetType="{x:Type Button}"/> 
    <Window.Resources> 

    <!-- this will use your implicit defined template --> 
    <Button /> 
    <!-- this will use your explicit defined template called myTemplate--> 
    <Button Template="{StaticResource myTemplate}"/> 
</Window>