第一種樣式的xaml按我的意願工作,使用setter生成帶有Wingding字形的按鈕以佈置內容及其屬性。這種風格的第二個版本試圖做同樣的事情,但是使用DataTemplate作爲Content,但它只是顯示DataTemplate的類型(即System.Windows.DataTemplate)。通過setter的樣式內容v ContentTemplate
- 爲什麼第二個版本不會顯示與第一個版本相同的內容?
- 假設修正是微不足道的,一個版本的風格比其他任何理由超出個人偏好的優先?
注:我顯示了綁定,並在情況下觸發有東西在那裏,是影響的內容,但它只是說變化
乾杯風格,
Berryl
樣式1
顯示器:
<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>
正在從TextBlock中刪除包裝DataTemplate解決問題? – LPL
@LPL。不,沒有DataTemplate包裝其運行時錯誤:錯誤'System.Windows.Controls.TextBlock'不是Setter上'System.Windows.Controls.ContentControl.ContentTemplate'屬性的有效值。 – Berryl
刪除DataTemplate適用於我。你應該把你的內容放入內容屬性中。不需要模板。 – Phil