2014-01-10 95 views
1

我基本上有一個ContentControl,它具有Border作爲內容的女巫也有TextBlock作爲內容。我想要的是例如TextBlock的Foreground Brush被綁定到父ContentControl的依賴項屬性...我被卡在這裏,我不知道如何解決這個問題。將依賴項屬性綁定到代碼中的子元素

public class NumberRollItem : ContentControl 
{ 
    public int Index { get; set; } 
    public int AnimationIndex { get; set; } 

    public Brush ItemForeground 
    { 
     get { return (Brush)GetValue(ItemForegroundProperty); } 
     set { SetValue(ItemForegroundProperty, value); } 
    } 
    public static readonly DependencyProperty ItemForegroundProperty = 
     DependencyProperty.Register("ItemForeground", typeof(Brush), typeof(NumberRollItem), new PropertyMetadata(new SolidColorBrush(Colors.White)));   
    public Brush ItemBackground 
    { 
     get { return (Brush)GetValue(ItemBackgroundProperty); } 
     set { SetValue(ItemBackgroundProperty, value); } 
    } 
    public static readonly DependencyProperty ItemBackgroundProperty = 
     DependencyProperty.Register("ItemBackground", typeof(Brush), typeof(NumberRollItem), new PropertyMetadata(new SolidColorBrush(Colors.Black))); 
    public double ItemFontSize 
    { 
     get { return (double)GetValue(ItemFontSizeProperty); } 
     set { SetValue(ItemFontSizeProperty, value); } 
    } 
    public static readonly DependencyProperty ItemFontSizeProperty = 
     DependencyProperty.Register("ItemFontSize", typeof(double), typeof(NumberRollItem), new PropertyMetadata(45d)); 



    public NumberRollItem(char c, int index) 
    { 
     this.Index = index; 
     string text = ""; text += c; 

     HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch; 
     Content = new Border() 
     { 
      Background = ItemBackground, // Background bound to ItemBackground but how?? 
      HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, 
      VerticalAlignment = System.Windows.VerticalAlignment.Stretch, 
      Child = new TextBlock() 
      { 
       FontSize = 45, 
       Text = text, 
       Foreground = ItemForeground, 
      }, 
     }; 
    } 
} 

回答

0

更新了WP

好吧,如果你通過代碼所做的一切,你可以在代碼中創建綁定過,

喜歡的東西:

public NumberRollItem(char c, int index) 
{ 
    this.Index = index; 
    string text = ""; text += c; 

    // Giving This control a Name to later use for Binding 
    string thisControlName = "NumberItem"; 

    Name = thisControlName; 

    HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch; 
    var tBlock = new TextBlock() 
    { 
    FontSize = 45, 
    Text = text 
    }; 
    var binding = new Binding 
    { 
    ElementName = thisControlName, 
    Path = new PropertyPath("ItemForeground") 
    }; 
    tBlock.SetBinding(TextBlock.ForegroundProperty, binding); 
    Content = new Border() 
    { 
    Background = ItemBackground, 
    HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, 
    VerticalAlignment = System.Windows.VerticalAlignment.Stretch, 
    Child = tBlock, 
    }; 
} 

什麼你正在用這個代碼是:

在t他的構造函數NumberRollItem您創建一個BindingElementName指向NumberRollItem類並將綁定分配給TextBlock.ForegroundProperty。因此,他們必然會允許將來對ItemForeground進行的更改也會影響TextBlock.Foreground

不知道爲什麼你已經採取了這種方法,而不是XAML Style路線,但無論雅的作品:)

+0

其實有在Windows的手機,但由於沒有RelativeSourceMode.FindAncestor反正。 –

+0

@ElMarchewko哦,我忘了這是WP。我已經更新了適用於Windows手機的答案。基本上只需要將'RelativeSource'綁定切換到一個簡單的''ElementName'',如更新後的答案所示。我已經檢查過它在模擬器上正常工作。 – Viv

相關問題