Is there a way to specify a custom dependency property's default binding mode and update trigger?
Asked Answered
T

2

72

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));
Thyratron answered 18/4, 2010 at 21:5 Comment(0)
R
117

When registering the property, initialize your metadata with:

new FrameworkPropertyMetadata
{
    BindsTwoWayByDefault = true,
    DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
}
Roid answered 18/4, 2010 at 21:22 Comment(6)
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.Thyratron
I modified my answer to show how to do it with an object initializer. Use that instead of a constructor.Roid
@DiegoMijelshon is there a way to allow only OneWay Binding mode ?Plait
Very Cool,I'm just hunting for implementing TwoWay mode by hand.Magnifico
@Thyratron DefaultUpdateSourceTrigger= UpdateSourceTrigger.PropertyChanged does the trickMehalick
Guess it's worth noting that it's not possible to change the default to any other mode than TwoWay. So OneTime and OneWayToSource are excluded. At least I didn't find a way.Yann
H
24

In the Dependency Property declaration it would look like this:

public static readonly DependencyProperty IsExpandedProperty = 
        DependencyProperty.Register("IsExpanded", typeof(bool), typeof(Dock), 
        new FrameworkPropertyMetadata(true,
            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
            OnIsExpandedChanged));

public bool IsExpanded
{
    get { return (bool)GetValue(IsExpandedProperty); }
    set { SetValue(IsExpandedProperty, value); }
}
Hargett answered 23/7, 2012 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.