2012-03-16 75 views
1

我在設置綁定時非常困難,我認爲這應該很簡單。非常感謝幫助。從XAML資源字典到類屬性的綁定

我有一個名爲FormResource.xaml的資源字典。在這個字典中包含一個ScrollView的樣式,我將模板用於其中。目的是我想要一個更寬的垂直滾動條。

<Style x:Key="LargeScrolling" TargetType="ScrollViewer"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ScrollViewer"> 
       <Grid Background="{TemplateBinding Background}"> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="*"/> 
         <RowDefinition Height="Auto"/> 
        </Grid.RowDefinitions> 
        <ScrollContentPresenter x:Name="ScrollContentPresenter" 
          Margin="{TemplateBinding Padding}" 
          ContentTemplate="{TemplateBinding ContentTemplate}"/> 
        <ScrollBar x:Name="PART_VerticalScrollBar" 
          Style="{StaticResource LargeVerticalScrollBar}" 
          Width="{Binding ElementName=MDTForm, Path=ScrollBarWidth}" 
          IsTabStop="False" 
          Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" 
          Grid.Column="1" Grid.Row="0" Orientation="Vertical" 
          ViewportSize="{TemplateBinding ViewportHeight}" 
          Maximum="{TemplateBinding ScrollableHeight}" 
          Minimum="0" 
          Value="{TemplateBinding VerticalOffset}" 
          Margin="0,-1,-1,-1"/> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

我有一個名爲FormControl的UserControl。

public class FormControl : UserControl 

我曾經有過這樣的一個XAML組元,其中我所要做的工作的部分類,但我有,因爲我得到的這個類中刪除XAML在另一個組裝和WPF做不允許您從另一個程序集中的部分類派生。

在FormControl中,我定義了一個ScrollBarWidth屬性。

public static readonly DependencyProperty ScrollBarWidthProperty = DependencyProperty.Register("ScrollBarWidth", typeof(double), typeof(FormControl)); 
    public double ScrollBarWidth 
    { 
     get { return (double)base.GetValue(ScrollBarWidthProperty); } 
     set { base.SetValue(ScrollBarWidthProperty, value); } 
    } 

當我在主申報了這是一個局部類我給FormControl類MDTForm,這是我在我的結合使用什麼作爲的ElementName的名稱。我試圖在FormClass.cs中註冊這個名字,但不管我做了什麼,滾動條都沒有拿起屬性值。

這裏是我在FormControl類中創建我的ScrollViewer的地方。

 _canvasScrollViewer = new ScrollViewer(); 
     _canvasScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto; 
     _canvasScrollViewer.VerticalAlignment = VerticalAlignment.Top; 
     _canvasScrollViewer.MaxHeight = Constants.ScrollViewMaxHeight; 
     _canvasScrollViewer.Style = (Style)FindResource("LargeScrolling"); 

我得到這個工作的唯一方法是綁定到一個靜態屬性。我用它來裝訂。

Width="{Binding Source={x:Static form:FormControl.ScrollBarWidthP}}" 

然後定義屬性。

public static double ScrollBarWidth { get; set; } 

不過,我不希望這是我可以同時加載多個FormControl對象,他們可能並不都具有相同的滾動條的寬度屬性。

回答

2

使用的RelativeSource代替綁定的ElementName的:

{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
     AncestorType={x:Type controls:FormControl}}, Path=ScrollBarWidth} 

這將走上可視樹在運行時查找包含ScrollViewer中,這同時解決了您的作用域和多實例發出父控件。

+0

謝謝你,這真棒,完美的作品。是否有可能讓它支持自動屬性而不是DependancyProperty?它只需要單向綁定,ScrollViewer將永遠不會更新FormControl類的屬性。它現在可以工作,這很好,但如果可能的話,我寧願不必使用DependancyProperty。 – WPFNewbie 2012-03-16 13:28:11

+0

它只需要一個DP,如果你分配一個綁定到它(在這種情況下是寬度),而不是在另一側 - 對於雙向綁定也是如此。在這種情況下,ScrollBarWidth可以是DP,使用INotifyPropertyChanged的屬性(如果更新需要在寬度上顯示),或者如果不需要更新,則只是任何普通屬性。 – 2012-03-16 14:16:16

+0

當我改變它只是在公共雙重ScrollBarWidth {get;組; }它不起作用。我沒有改變任何將綁定聲明,所以也許需要改變。 – WPFNewbie 2012-03-16 19:04:35