2012-06-16 54 views
6

第一種樣式的xaml按我的意願工作,使用setter生成帶有Wingding字形的按鈕以佈置內容及其屬性。這種風格的第二個版本試圖做同樣的事情,但是使用DataTemplate作爲Content,但它只是顯示DataTemplate的類型(即System.Windows.DataTemplate)。通過setter的樣式內容v ContentTemplate

  1. 爲什麼第二個版本不會顯示與第一個版本相同的內容?
  2. 假設修正是微不足道的,一個版本的風格比其他任何理由超出個人偏好的優先?

注:我顯示了綁定,並在情況下觸發有東西在那裏,是影響的內容,但它只是說變化

乾杯風格,
Berryl

的第一部分

樣式1

顯示器:enter image description here

<Style x:Key="EditCommandButtonStyle" TargetType="{x:Type Button}" > 
    <Setter Property="Content" Value="a" /> 
    <Setter Property="Foreground" Value="Navy" /> 
    <Setter Property="FontFamily" Value="Wingdings 3" /> 
    <Setter Property="FontWeight" Value="Bold" /> 
    <Setter Property="FontSize" Value="18" /> 
    <Setter Property="Width" Value="30" /> 
    <Setter Property="Height" Value="Auto" /> 

    <!--What makes it an Edit button--> 
    <Setter Property="Command" Value="{Binding ActivateThisSatelliteVmCommand}"/> 
    <Setter Property="ToolTip"> 
     <Setter.Value> 
      <TextBlock> 
       <TextBlock.Text> 
        <Binding Path="HeaderLabel" StringFormat="{resx:Resx ResxName=Smack.Core.Presentation.Resources.MasterDetail, Key=Item_Edit_Label}"/> 
       </TextBlock.Text> 
      </TextBlock> 
     </Setter.Value> 
    </Setter> 

    <!-- WHen its available --> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Button}"> 
       <Border x:Name="theBorder" CornerRadius="4"> 
        <ContentPresenter x:Name="theContent" VerticalAlignment="Center" HorizontalAlignment="Center" /> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsMouseOver" Value="False"> 
         <Setter TargetName="theContent" Property="Visibility" Value="Hidden"/> 
         <Setter TargetName="theBorder" Property="Background" Value="Transparent"/> 
        </Trigger> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter TargetName="theContent" Property="Visibility" Value="Visible"/> 
        </Trigger> 
        <Trigger Property="IsPressed" Value="True"> 
         <Setter TargetName="theContent" Property="Visibility" Value="Visible"/> 
         <Setter TargetName="theBorder" Property="Background" Value="Orange"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

樣式2

顯示 「System.Windows.DataTemplate」

<Style x:Key="EditCommandButtonStyle" TargetType="{x:Type Button}" > 
    <Setter Property="Content"> 
     <Setter.Value> 
      <DataTemplate> 
       <TextBlock Text="a" FontFamily="Wingdings 3" FontWeight="Bold" FontSize="18" Foreground="Navy" /> 
      </DataTemplate> 
     </Setter.Value> 
    </Setter> 

    <!--What makes it an Edit button--> 
    <Setter Property="Command" Value="{Binding ActivateThisSatelliteVmCommand}"/> 
    <Setter Property="ToolTip"> 
     <Setter.Value> 
      <TextBlock> 
       <TextBlock.Text> 
        <Binding Path="HeaderLabel" StringFormat="{resx:Resx ResxName=Core.Presentation.Resources.MasterDetail, Key=Item_Edit_Label}"/> 
       </TextBlock.Text> 
      </TextBlock> 
     </Setter.Value> 
    </Setter> 

    <!-- When its available --> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Button}"> 
       <Border x:Name="theBorder" CornerRadius="4"> 
        <ContentPresenter x:Name="theContent" VerticalAlignment="Center" HorizontalAlignment="Center" /> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsMouseOver" Value="False"> 
         <Setter TargetName="theContent" Property="Visibility" Value="Hidden"/> 
         <Setter TargetName="theBorder" Property="Background" Value="Transparent"/> 
        </Trigger> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter TargetName="theContent" Property="Visibility" Value="Visible"/> 
        </Trigger> 
        <Trigger Property="IsPressed" Value="True"> 
         <Setter TargetName="theContent" Property="Visibility" Value="Visible"/> 
         <Setter TargetName="theBorder" Property="Background" Value="Orange"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

正在從TextBlock中刪除包裝DataTemplate解決問題? – LPL

+0

@LPL。不,沒有DataTemplate包裝其運行時錯誤:錯誤'System.Windows.Controls.TextBlock'不是Setter上'System.Windows.Controls.ContentControl.ContentTemplate'屬性的有效值。 – Berryl

+0

刪除DataTemplate適用於我。你應該把你的內容放入內容屬性中。不需要模板。 – Phil

回答

14

Content屬性被設置爲一個DataTemplate

DataTemplates意指與Template特性一起使用,而不是直接插入到通過Content屬性的VisualTree

更改您的樣式設置器以設置ContentTemplate,而不是Content,它應該正常工作

<Setter Property="ContentTemplate"> 
    <Setter.Value> 
     <DataTemplate> 
      <TextBlock Text="a" FontFamily="Wingdings 3" FontWeight="Bold" FontSize="18" Foreground="Navy" /> 
     </DataTemplate> 
    </Setter.Value> 
</Setter> 

關於你的第二個問題,我更喜歡第一個,因爲它的簡單,我認爲它可能包含在Visual樹較少的元素(我不得不雙精度檢查)

+6

賓果,毫不奇怪。它證實了我的懷疑,即只有極少數人始終如一地回答那些應該是世俗問題的問題時,WPF學習曲線驚人地陡峭。乾杯 – Berryl