Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Programmatic Binding on a BitmapEffect

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.

like image 889
viggity Avatar asked Sep 12 '08 21:09

viggity


1 Answers

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.

like image 191
Whisk Avatar answered Nov 15 '22 06:11

Whisk