Reputation: 60486
Im new to WPF / Silverlight so its hard for me to describe what i try to do. Maybe that is the reason i could not find a answer on Stackoverflow and Google.
I try to bind to a DependyProperty
programmatically.
public static DependencyProperty MyDependencyProperty
= DependencyProperty.RegisterAttached(
"...",
typeof(...),
typeof(...),
new PropertyMetadata(...)
);
xmlns:MyXMLNS="clr-namespace:...."
<ListBox MyXMLNS:MyClass.MyDependencyProperty="...">
// ....
</ListBox>
This already works as expected.
How can i get this done programmatically?
Upvotes: 1
Views: 2264
Reputation: 184607
The format in code is always the same and just a non-verbatim translation if you know how the XAML is handled.
<ListBox local:Attached.Test="{Binding PathToProperty)"/>
var binding = new Binding("PathToProperty");
listBox.SetBinding(Attached.TestProperty, binding);
If you set other properties on the binding like ElementName you should set those before SetBinding
. (This SetBinding
method is just for convenience (if you only set the Binding.Path
there even is another one), for non-FrameworkElements you need BindingOperations.SetBinding
)
Upvotes: 3