2012-07-09 43 views
2

我剛剛創建的擴展風格,在頭一個複選框:如何將新的屬性添加到控件模板/風格

<Style TargetType="{x:Type Expander}" x:Key="MyCheckboxExpander"> 
    <Setter Property="Background" Value="{StaticResource ExpanderBackgroundBrush}"/> 
    <Setter Property="HeaderTemplate" Value="{StaticResource MyHeaderExpander}" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Expander}"> 
       <Grid> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="Auto"/> 
         <RowDefinition Height="*" x:Name="ContentRow"/> 
        </Grid.RowDefinitions> 
        <Border Grid.Row="0" x:Name="Border" Background="{TemplateBinding Background}" BorderThickness="1" 
          BorderBrush="{StaticResource ExpanderBorderBrush}" CornerRadius="2,2,0,0"> 
         <Grid> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="18"/> 
           <ColumnDefinition Width="*"/> 
           <ColumnDefinition Width="20"/> 
          </Grid.ColumnDefinitions> 
          <CheckBox VerticalAlignment="Center" Margin="4,0,0,2"/> 
          <ToggleButton Grid.Column="2" x:Name="ToggleBt" Template="{DynamicResource MyExpanderToggleButton}" Background="{StaticResource ExpanderBackgroundBrush}" 
              IsChecked="{Binding Path=IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" 
              OverridesDefaultStyle="True"/> 
          <ContentPresenter Grid.Column="1" Margin="0,0,0,0" RecognizesAccessKey="True" ContentSource="Header"/> 
         </Grid> 
        </Border> 
        <Border Visibility="Collapsed" Grid.Row="1" x:Name="ExpandSite" Background="{StaticResource ContentBackgroundBrush}" 
          BorderBrush="{StaticResource ExpanderBorderBrush}" BorderThickness="1,0,1,1" CornerRadius="0,0,2,2"> 
         <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" 
              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Focusable="false"/> 
        </Border> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsExpanded" Value="True"> 
         <Setter Property="Visibility" Value="Visible" TargetName="ExpandSite"/> 
        </Trigger> 
        <Trigger Property="IsEnabled" Value="False"> 
         <Setter Property="Background" Value="{StaticResource ExpanderBackgroundBrush}" TargetName="Border"/> 
         <Setter Property="BorderBrush" Value="{StaticResource ExpanderBackgroundBrush}" TargetName="Border"/> 
         <Setter Property="Visibility" Value="Hidden" TargetName="ToggleBt" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

我怎樣才能爲這個擴展,以創建一個新的屬性(器isChecked)綁定到一個布爾值?即G:

<Expander IsChecked="{Binding MyBoolValue}" /> 

而且在模板:

<CheckBox VerticalAlignment="Center" Margin="4,0,0,2" IsChecked="{TemplateBinding ????}" /> 

編輯:

我發現了一個similar question但我想知道是否有另一種方式來做到這一點,而無需使用控制繼承。

+0

想知道如果一個靜態附加屬性可能工作 - 不知道雖然...試一試可能嗎? – Charleh 2012-07-09 16:36:29

+0

是的,我剛剛讀到它... – 2012-07-09 16:40:02

回答

2

我創建了一個附加屬性是這樣的:

public sealed class AP : DependencyObject 
{ 
    public static bool GetIsChecked(DependencyObject obj) 
    { 
     return (bool)obj.GetValue(IsCheckedProperty); 
    } 

    public static void SetIsChecked(DependencyObject obj, bool value) 
    { 
     obj.SetValue(IsCheckedProperty, value); 
    } 

    // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty IsCheckedProperty = 
     DependencyProperty.RegisterAttached("IsChecked", typeof(bool), typeof(AP), new UIPropertyMetadata(false)); 
} 

然後,所有我需要做的就是:

<Expander my:AP.IsChecked="{Binding Path=TestBool}" /> 

,並且模板:

<CheckBox VerticalAlignment="Center" Margin="4,0,0,2" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(my:AP.IsChecked)}" /> 

我不知道該怎麼如果樣式是在不同的裝配難度中定義的,請執行...

相關問題