views:

1719

answers:

1

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.

Thanks in advance.

+6  A: 

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.

Whisk