WPF - bitmap software binding

I would like to be able to programmatically associate some data with dependency properties in BitmapEffect . With a FrameworkElement element, such as TextBlock, there is a SetBinding method where you can programmatically perform these bindings, for example:

myTextBlock.SetBinding(TextBlock.TextProperty, new Binding("SomeProperty"));

And I know that you can do it in direct XAML (as shown 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 do this with C #, because BitmapEffect does not have a SetBinding method.

I tried:

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

But that will not work.

+5
source share
1 answer

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 you need to do what you want.

+11

All Articles