2011-09-03 100 views
3

請原諒我,作爲一個完整的新手,但我下面的教程(Creating a Custom WPF Button Template in XAML),和我遇到了錯誤:住宅「模板」式「FrameworkElement的」未找到

FormatException was thrown due to document error: Property 'Template' was not found in type 'FrameworkElement'.

看來,誤差在XAML下面的代碼莖:

<Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="Button"> 
      <Border Name="border" 
       BorderThickness="1" 
       Padding="4,2" 
       BorderBrush="DarkGray" 
       CornerRadius="3" 
       Background="{TemplateBinding Background}"> 
       <Grid > 
       <ContentPresenter HorizontalAlignment="Center" 
          VerticalAlignment="Center" Name="contentShadow" 
        Style="{StaticResource ShadowStyle}"> 
        <ContentPresenter.RenderTransform> 
         <TranslateTransform X="1.0" Y="1.0" /> 
        </ContentPresenter.RenderTransform> 
       </ContentPresenter> 
       <ContentPresenter HorizontalAlignment="Center" 
         VerticalAlignment="Center" Name="content"/> 
       </Grid> 
     </Border> 

我一直在到處找一個解決方案,但還沒有找到一個...這使我相信,我是無論是俯視還是過度俯視。

我缺少什麼?預先感謝您的任何幫助!

+0

請參閱_Download the example XAML file_ in that link for the complete self-contained example。它顯示應該插入您引用的XAML片段的位置。 –

回答

3

A FrameworkElement沒有Template屬性。模板通常在Control類中定義。這是因爲大多數WPF元素來自FrameworkElement,但它們並不都具有模板(例如,StackPanel)。你的二傳可能在一個風格(你沒有發佈該部分)。確保StyleTargetType是正確的類型(最可能是Button)。

<Style x:Key="InformButton" TargetType="Button"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <Border Name="border" 
       etc... 
</Style> 
+0

哇..感謝NotDan!你在現場。 TargetType是我所缺少的。非常感謝解釋! – PsiLentRain

+0

我正在尋找一個共享的「背景」屬性,它也不存在於'FrameworkElement'中。感謝您指出'Control'類。 – Mehrad