WPF - Programmatic Binding on a BitmapEffect
Asked Answered
H

1

5

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.

Histopathology answered 12/9, 2008 at 21:5 Comment(0)
J
11

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.

Janeenjanek answered 13/9, 2008 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.