WPF - BitmapEffect的程序化绑定 [英] WPF - Programmatic Binding on a BitmapEffect

查看:149
本文介绍了WPF - BitmapEffect的程序化绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够以编程方式将某些数据绑定到BitmapEffect 的依赖属性。使用FrameworkElement(如TextBlock)有一个SetBinding方法,您可以通过编程方式执行这些绑定,如:

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"));

我知道你可以直接使用XAML(如下所示)

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>

但是,我无法弄清楚如何使用C#来完成此任务,因为BitmapEffect没有SetBinding方法。

But I can't figure out how to accomplish this with C# because BitmapEffect doesn't have a SetBinding method.

我试过:

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

但是不行。

推荐答案

您可以使用 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.

这篇关于WPF - BitmapEffect的程序化绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆