Reputation: 35
My application generates a dynamic number of styles in code-behind. I'd like to bind a specific property of those styles to a dependency property. It's possible in XAML, but I found no way to do it in code-behind. Since Setter is no FrameworkElement, it does not provide a SetBinding() method. And since Setter.Value is no dependency property BindingOperations.SetBinding() won't work either.
How does
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="{Binding FontSize}"/>
</Style>
look in code-behind?
Upvotes: 1
Views: 1198
Reputation: 50682
Like this:
this.DataContext = new Thing { FontSize = 5.5 };
Style style = new Style(typeof(TextBlock));
style.Setters.Add(
new Setter(TextBlock.FontSizeProperty, new Binding("FontSize")));
textBlock1.Style = style;
Upvotes: 1