WPF - ربط برمجي على تأثير BitmapEffect
-
09-06-2019 - |
سؤال
أود أن أكون قادرًا على ذلك برمجيا ربط بعض البيانات بخصائص التبعية على a تأثير نقطي.باستخدام FrameworkElement مثل TextBlock، توجد طريقة 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>
لكن لا يمكنني معرفة كيفية تحقيق ذلك باستخدام لغة C# لأن BitmapEffect لا يحتوي على طريقة SetBinding.
لقد حاولت:
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);
أعتقد أن هذا يجب أن يفعل ما تريد.
لا تنتمي إلى StackOverflow