2008-09-12 64 views
5

綁定我想能夠編程綁定一些數據的依賴性質上BitmapEffect。有了這樣的TextBlock一個FrameworkElement的有一個SetBinding方法,在那裏你可以以編程方式執行這些綁定,如:WPF - 編程上BitmapEffect

myTextBlock.SetBinding(TextBlock.TextProperty, new Binding("SomeProperty")); 

而且我知道你可以在直XAML做(如下圖所示)

<TextBlock Width="Auto" Text="Some Content" x:Name="MyTextBlock" TextWrapping="Wrap" > 
    <TextBlock.BitmapEffect> 
     <BitmapEffectGroup> 
      <OuterGlowBitmapEffect x:Name="MyGlow" GlowColor="White" GlowSize="{Binding Path=MyValue}" /> 
     </BitmapEffectGroup> 
    </TextBlock.BitmapEffect> 
</TextBlock> 

但我無法弄清楚如何用C#實現這一點,因爲BitmapEffect沒有SetBinding方法。

我已經試過:

myTextBlock.SetBinding(OuterGlowBitmapEffect.GlowSize, new Binding("SomeProperty") { Source = someObject }); 

但它不工作。

回答

11

您可以使用BindingOperation.SetBinding

Binding newBinding = new Binding(); 
newBinding.ElementName = "SomeObject"; 
newBinding.Path = new PropertyPath(SomeObjectType.SomeProperty); 
BindingOperations.SetBinding(MyGlow, OuterGlowBitmapEffect.GlowSizeProperty, newBinding); 

我認爲應該做你想要什麼。