vig*_*ity 5 data-binding wpf bitmapeffect
我希望能够以编程方式将一些数据绑定到BitmapEffect上的依赖项属性.使用像TextBlock这样的FrameworkElement,有一个SetBinding方法,您可以在其中以编程方式执行以下绑定:
myTextBlock.SetBinding(TextBlock.TextProperty, new Binding("SomeProperty"));
Run Code Online (Sandbox Code Playgroud)
我知道你可以用直接的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>
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何用C#实现这一点,因为BitmapEffect没有SetBinding方法.
我试过了:
myTextBlock.SetBinding(OuterGlowBitmapEffect.GlowSize, new Binding("SomeProperty") { Source = someObject });
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
Whi*_*isk 11
您可以使用BindingOperation.SetBinding:
Binding newBinding = new Binding();
newBinding.ElementName = "SomeObject";
newBinding.Path = new PropertyPath(SomeObjectType.SomeProperty);
BindingOperations.SetBinding(MyGlow, OuterGlowBitmapEffect.GlowSizeProperty, newBinding);
Run Code Online (Sandbox Code Playgroud)
我认为应该做你想做的事.