2009-06-10 360 views
6

設計繼承的擴展器時遇到問題。我的意圖是在切換按鈕後面有一個進度條,並在默認的擴展器頭文件中有文本。將自定義依賴屬性綁定到自定義WPF樣式

我有這個XAML代碼,它給了我頭部的進度條。這是一種自定義風格。

<Style x:Key="CurrentScanExpanderStyle" TargetType="{x:Type local:ProgressExpander}"> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
     <Setter Property="VerticalContentAlignment" Value="Stretch"/> 
     <Setter Property="BorderBrush" Value="Transparent"/> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type local:ProgressExpander}"> 
        <Border SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3"> 
         <DockPanel> 
          <Grid DockPanel.Dock="Top"> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="*"/> 
           </Grid.ColumnDefinitions> 
           <ProgressBar Name="ProgressBar"/> 
           <ToggleButton FontFamily="{TemplateBinding FontFamily}" FontSize="{TemplateBinding FontSize}" FontStretch="{TemplateBinding FontStretch}" FontStyle="{TemplateBinding FontStyle}" FontWeight="{TemplateBinding FontWeight}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" FocusVisualStyle="{StaticResource ExpanderHeaderFocusVisual}" Margin="1" MinHeight="0" MinWidth="0" x:Name="HeaderSite" Style="{StaticResource ExpanderDownHeaderStyle}" IsChecked="{Binding Path=IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Content="{TemplateBinding Header}" ContentTemplate="{TemplateBinding HeaderTemplate}" ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"/> 
          </Grid> 
          <ContentPresenter Focusable="false" Visibility="Collapsed" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" x:Name="ExpandSite" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" DockPanel.Dock="Bottom"/> 
         </DockPanel> 
        </Border> 
        <ControlTemplate.Triggers> 
         <!-- Triggers haven't changed from the default --> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

這工作正常,但我有麻煩約束我的自定義依賴項屬性,它控制進度百分比。

public class ProgressExpander : Expander 
{ 
    static ProgressExpander() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(ProgressExpander), new FrameworkPropertyMetadata(typeof(ProgressExpander))); 
    }  



    public int Progress 
    { 
     get { return (int)GetValue(ProgressProperty); } 
     set { SetValue(ProgressProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for Progress. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty ProgressProperty = 
     DependencyProperty.Register("Progress", typeof(int), typeof(ProgressExpander), new UIPropertyMetadata(0)); 


} 

這是窗口中的代碼:

  <local:ProgressExpander Grid.Row="1" Header="Current Scan" ExpandDirection="Down" x:Name="currentScanExpander" Style="{DynamicResource CurrentScanExpanderStyle}"> 
       <Canvas Background="SkyBlue" 
         Name="currentScanCanvas" 
         Height="{Binding ElementName=currentScanExpander, Path=ActualWidth}" 
         /> 
      </local:ProgressExpander> 

我不知道如何綁定這個依賴屬性進度的樣式內的進度的進度值。

任何幫助,將不勝感激。

回答

12

在樣式中,我們可以使用帶有RelativeSource的標準綁定來設置屬性。

<ProgressBar Name="ProgressBar" 
      Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Progress}" 
      Minimum="0" 
      Maximum="100" /> 

然後,在窗口中,我們只需添加進展=「50」或結合到別的地方。

您還需要將Button的背景設置爲透明,或者更改佈局的某種方式才能看到它。

+1

也許可以使用TemplateBinding;少打字,再加上這就是TemplateBinding的用途。 http://msdn.microsoft.com/en-us/library/ms742882.aspx – 2009-06-10 03:28:30

相關問題