views:

117

answers:

1

I would like to make it so that, as default, when I bind to one of my dependency properties the binding mode is two-way and update-trigger is property changed. Is there a way to do this?

Here is an example of one of my dependency properties:

public static readonly DependencyProperty BindableSelectionLengthProperty =
        DependencyProperty.Register(
        "BindableSelectionLength",
        typeof(int),
        typeof(ModdedTextBox),
        new PropertyMetadata(OnBindableSelectionLengthChanged));
+4  A: 

When registering the property, initialize your metadata with:

new FrameworkPropertyMetadata
{
    BindsTwoWayByDefault = true,
    DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
}
Diego Mijelshon
I was able to set BindsTwoWayByDefault by adding this to my example dp:new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnBindableSelectionStartChanged). However, I am still having trouble setting the UpdateSourceTrigger to PropertyChanged.
Justin
I modified my answer to show how to do it with an object initializer. Use that instead of a constructor.
Diego Mijelshon
Thanks for your help Diego!
Justin