WPF - Programmatic Binding on a BitmapEffect

ぃ、小莉子 提交于 2019-12-10 14:10:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!