Question

j'aimerais pouvoir par programmation lier certaines données aux propriétés de dépendance sur un Effet Bitmap.Avec un FrameworkElement comme TextBlock, il existe une méthode SetBinding où vous pouvez effectuer ces liaisons par programme comme :

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

Et je sais que vous pouvez le faire en XAML direct (comme vu ci-dessous)

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

Mais je n'arrive pas à comprendre comment y parvenir avec C# car BitmapEffect n'a pas de méthode SetBinding.

J'ai essayé:

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

Mais ça ne marche pas.

Était-ce utile?

La solution

Vous pouvez utiliser BindingOperation.SetBinding:

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

Je pense que cela devrait faire ce que tu veux.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top