質問

できるようになりたいです プログラム的に 一部のデータを依存関係プロパティにバインドします。 ビットマップ効果. 。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 });

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

役に立ちましたか?

解決

使用できます BindingOperation.SetBinding:

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

それはあなたが望むようにすべきだと思います。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top