Binding Setter.Value from code
In XAML, I can write something like this:
<Setter Property="PropertyName" Value="{Binding ...}" /> How do I do this in code? I already created bindings in the code, but I can not find the static ValueProperty object in the Setter class to go to BindingOperations.SetBinding() .
When setting up binding on the setter, you don't need BindingOperations at all. All you have to do is:
var setter = new Setter(TextBlock.TextProperty, new Binding("FirstName")); or equivalent
var setter = new Setter { Property = TextBlock.TextProperty, Value = new Binding("FirstName"), }; any of them would be equivalent
<Setter Property="TextBlock.Text" Value="{Binding FirstName}" /> The reason for this is that Setter.Value is a common CLR property, not a DependencyProperty, and therefore it cannot be bound. Thus, there is no ambiguity in either XAML or code when you store a Binding object in it.
When a setter is actually applied to an object, if a binding is found in Setter, the equivalent of BindingOperations.SetBinding is called. Otherwise, the property is set directly.