我希望能够将一些数据绑定到programmatically BitmapEffect上的依赖项属性。对于类似于FrameworkElement的TextBlock,有一个SetBinding方法,您可以通过编程方式这样做这些绑定:
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 });但不起作用。
发布于 2008-09-13 09:16:15
您可以使用BindingOperation.SetBinding
Binding newBinding = new Binding();
newBinding.ElementName = "SomeObject";
newBinding.Path = new PropertyPath(SomeObjectType.SomeProperty);
BindingOperations.SetBinding(MyGlow, OuterGlowBitmapEffect.GlowSizeProperty, newBinding);我觉得那应该能做你想做的事。
https://stackoverflow.com/questions/59958
复制相似问题