2014-02-13 48 views
0

有沒有辦法與該複選框(未TextBlock的)複選框類型/格式

<CheckBox> 
    <TextBlock Text="All" Margin="-1,1,0,0" /> 
</CheckBox> 

例如一種風格來實現這一

<CheckBox Content="All" Style="{StaticResource CloseText}" /> 

基於回答帕夏
填充不動
這裏的問題是樣式應用於所有屬性,但填充
參見下面的文字:CB1和CB2不具有相同的填充
的ContentTemplate做工作
是否可以在Button樣式中包含ContentTemplate?

<Window x:Class="CheckBoxStyle.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <Style TargetType="CheckBox"> 
      <Setter Property="FontFamily" Value="Segoe UI" /> 
      <Setter Property="FontStyle" Value="Italic" /> 
      <Setter Property="Margin" Value="50,2,0,0" /> 
      <Setter Property="Padding" Value="-1,0,0,0" /> 
     </Style> 
     <Style TargetType="CheckBox" x:Key="CheckBox01"> 
      <Setter Property="FontFamily" Value="Courier" /> 
      <Setter Property="FontStyle" Value="Oblique" /> 
      <Setter Property="Margin" Value="40,2,0,0" /> 
      <Setter Property="Padding" Value="10,0,0,0" /> 
     </Style> 
     <DataTemplate x:Key="CloseText"> 
      <TextBlock Text="{Binding}" Margin="-3,1,0,0" /> 
     </DataTemplate> 
    </Window.Resources> 
    <Grid> 
     <StackPanel Orientation="Vertical"> 
      <CheckBox Content="CB1" /> 
      <CheckBox Content="CB2" Padding="-1,0,0,0" /> 
      <CheckBox Content="CB3" Style="{StaticResource CheckBox01}" /> 
      <CheckBox Content="CB4" Style="{StaticResource CheckBox01}" ContentTemplate="{StaticResource CloseText}"/> 
     </StackPanel>  
    </Grid> 
</Window> 

回答

2
<Window.Resources> 
    <DataTemplate x:Key="CloseText"> 
     <TextBlock Text="{Binding}" Margin="-1,1,0,0" /> 
    </DataTemplate> 
</Window.Resources> 

...

<CheckBox Content="All" ContentTemplate="{StaticResource CloseText}"/> 

爲複選框樣式(內聯或資源引用)的一部分:

<DataTemplate x:Key="CloseText"> 
     <TextBlock Text="{Binding}" Margin="-3,1,0,0" /> 
    </DataTemplate> 
    <Style TargetType="CheckBox"> 
     <Setter Property="FontFamily" Value="Segoe UI" /> 
     <Setter Property="FontStyle" Value="Italic" /> 
     <Setter Property="Margin" Value="50,2,0,0" /> 
     <Setter Property="Padding" Value="-1,0,0,0" /> 
     <Setter Property="ContentTemplate"> 
      <Setter.Value> 
       <DataTemplate> 
        <TextBlock Text="{Binding}" Margin="-3,1,0,0" /> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
    <Style TargetType="CheckBox" x:Key="CheckBox01"> 
     <Setter Property="FontFamily" Value="Courier" /> 
     <Setter Property="FontStyle" Value="Oblique" /> 
     <Setter Property="Margin" Value="40,2,0,0" /> 
     <Setter Property="Padding" Value="10,0,0,0" /> 
     <Setter Property="ContentTemplate" Value="{StaticResource CloseText}" /> 
    </Style> 
+0

就最後一個問題吧。是否可以在按鈕樣式中包含DataTemplate> – Paparazzi

+0

是的,只需在樣式之前聲明DataTemplate併爲其添加setter – PashaPash