5

一部のデータをBitmapEffectの依存関係プロパティにプログラムでバインドできるようにしたいと考えています。TextBlock のような FrameworkElement には、次のようなバインディングをプログラムで実行できる SetBinding メソッドがあります。

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

そして、ストレート XAML で実行できることはわかっています (以下を参照)。

<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>

しかし、BitmapEffect には SetBinding メソッドがないため、C# でこれを実現する方法がわかりません。

私はもう試した:

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

しかし、うまくいきません。

4

1 に答える 1

11

BindingOperation.SetBindingを使用できます。

Binding newBinding = new Binding();
newBinding.ElementName = "SomeObject";
newBinding.Path = new PropertyPath(SomeObjectType.SomeProperty);
BindingOperations.SetBinding(MyGlow, OuterGlowBitmapEffect.GlowSizeProperty, newBinding);

それはあなたが望むことをするべきだと思います。

于 2008-09-13T09:16:15.607 に答える