问题
I would like to be able to programmatically bind some data to the dependency properties on a BitmapEffect. With a FrameworkElement like TextBlock there is a SetBinding method where you can programmatically do these bindings like:
myTextBlock.SetBinding(TextBlock.TextProperty, new Binding("SomeProperty"));
And I know you can do it in straight XAML (as seen below)
<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>
But I can't figure out how to accomplish this with C# because BitmapEffect doesn't have a SetBinding method.
I've tried:
myTextBlock.SetBinding(OuterGlowBitmapEffect.GlowSize, new Binding("SomeProperty") { Source = someObject });
But it doesn't work.
回答1:
You can use BindingOperation.SetBinding:
Binding newBinding = new Binding();
newBinding.ElementName = "SomeObject";
newBinding.Path = new PropertyPath(SomeObjectType.SomeProperty);
BindingOperations.SetBinding(MyGlow, OuterGlowBitmapEffect.GlowSizeProperty, newBinding);
I think that should do what you want.
来源:https://stackoverflow.com/questions/59958/wpf-programmatic-binding-on-a-bitmapeffect