62

In WPF Binding.Mode, when selecting Default, it depends in the property being binded.

I am looking for some list or some convention or any information for the defaults for the various controls.
I mean, what properties are TwoWay by default and so on. Any links, ideas, thoughts and even rants are welcommed!

Tobias Hoefer
  • 1,058
  • 12
  • 29
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632

3 Answers3

77

Similar to UpdateSourceTrigger, the default value for the Mode property varies for each property. User-editable properties such as TextBox.Text, ComboBox.Text, MenuItem.IsChecked, etc, have TwoWay as their default Mode value. To figure out if the default is TwoWay, look at the Dependency Property Information section of the property. If it says BindsTwoWayByDefault is set to true, then the default Mode value of the property is TwoWay. To do it programmatically, get the property metadata of the property by calling GetMetadata and then check the boolean value of the BindsTwoWayByDefault property.

Source: https://web.archive.org/web/20100209025938/http://blogs.msdn.com/wpfsdk/archive/2006/10/19/wpf-basic-data-binding-faq.aspx

The safest way would be to always be explicit what kind of binding mode you want from a binding.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Lars Truijens
  • 42,837
  • 6
  • 126
  • 143
  • 2
    Is there a place where I can find a list of all the properties and their BindingMode defaults? what are the rules? – Shimmy Weitzhandler Aug 05 '10 at 17:56
  • The list of properties is endless. I guess http://msdn.microsoft.com lists most of the ones from Microsoft. I don't see any information and the binding mode there so I guess you would have to find that out by yourself. The url that I gave in my answer explains how to find them – Lars Truijens Aug 05 '10 at 18:26
  • 3
    What about dependency properties whose metadata are UIPropertyMetadata rather than FrameworkPropertyMetadata? The MSDN docs for such properties don't mention BindsTwoWayByDefault since that boolean doesn't exist on UIPropertyMetadata. – HappyNomad Jan 04 '11 at 15:27
  • Not a list but the properties that use two-way binding by default have this mentioned in the `Dependency Property Information` segment. There is an `BindsTwoWayByDefault` entry in the `Metadata properties set to true` row. – Tim Pohlmann Feb 27 '17 at 09:49
11

Here's a way to find the Default mode supported by a DP -

.NET Reflector is your friend. With reflector, search for TextBox and look at the source for the static constructor (.cctor()). Here, you will be able to find the code used for registering the TextProperty DP:

TextProperty = DependencyProperty.Register
               (
                   "Text", 
                   typeof(string), 
                   typeof(TextBox), 
                   new FrameworkPropertyMetadata
                   (
                      string.Empty, 
                      FrameworkPropertyMetadataOptions.Journal |
                      FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, 
                      new PropertyChangedCallback(TextBox.OnTextPropertyChanged), 
                      new CoerceValueCallback(TextBox.CoerceText), 
                      true, 
                      UpdateSourceTrigger.LostFocus
                   )
                );

Notice that a parameter is passed to the Register method indicating the default Binding Mode: FrameworkPropertyMetadataOptions.BindsTwoWayByDefault. If you use reflector to look at the registration for TextBlock’s Text DP, you will see that no such value is passed, in which case we assume the binding is one way by default.

Taken from Bea Stollnitz's post : How can I update an explicit binding within a template?

Although having some kind of list of important DP's would be very helpful.

akjoshi
  • 15,374
  • 13
  • 103
  • 121
  • 1
    Its also easy to lookup the reference source: https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/windows/Controls/TextBox.cs,47e834d7646f17c6,references – StayOnTarget Apr 24 '20 at 19:31
3

Was looking for a list as well, mostly to find out which bindings could be set to one-way to improve performance. The following functions can help you find which controls use two-way binding by default:

public IList<DependencyProperty> GetAttachedProperties(DependencyObject obj)
{
    var result = new List<DependencyProperty>();
    foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(obj, new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.Valid) }))
    {
        var dpd = DependencyPropertyDescriptor.FromProperty(pd);
        if (dpd != null)
        {
            result.Add(dpd.DependencyProperty);
        }
    }
    return result;
}

public bool IsBindsTwoWayByDefault(DependencyObject obj, DependencyProperty property)
{
    var metadata = property.GetMetadata(obj) as FrameworkPropertyMetadata;
    if (metadata != null)
    {
        return metadata.BindsTwoWayByDefault;
    }
    return false;
}

Using a print function, gives us a list:

var objList = new List<DependencyObject> { new TextBox(), new TextBlock(), new Label(), new ComboBox(), new Button() };
foreach (var obj in objList)
{
    var props = GetAttachedProperties(obj);
    foreach (var prop in props)
    {
        if(IsBindsTwoWayByDefault(obj, prop))
            Debug.WriteLine($"{obj} : {prop.OwnerType}:{prop.Name}");
    }
}

Sample result (control properties with two-way binding as default)

System.Windows.Controls.TextBox : System.Windows.Controls.TextBox:Text
System.Windows.Controls.TextBox : System.Windows.Controls.TextSearch:Text
System.Windows.Controls.TextBlock : System.Windows.Controls.TextSearch:Text
System.Windows.Controls.Label : System.Windows.Controls.TextSearch:Text
System.Windows.Controls.ComboBox Items.Count:0 : System.Windows.Controls.ComboBox:IsDropDownOpen
System.Windows.Controls.ComboBox Items.Count:0 : System.Windows.Controls.ComboBox:Text
System.Windows.Controls.ComboBox Items.Count:0 : System.Windows.Controls.Primitives.Selector:SelectedIndex
System.Windows.Controls.ComboBox Items.Count:0 : System.Windows.Controls.Primitives.Selector:SelectedItem
System.Windows.Controls.ComboBox Items.Count:0 : System.Windows.Controls.Primitives.Selector:SelectedValue
System.Windows.Controls.ComboBox Items.Count:0 : System.Windows.Controls.TextSearch:Text
System.Windows.Controls.Button : System.Windows.Controls.TextSearch:Text

Interestingly, most controls have a TextSearch property which has two-way binding.

Freek Sanders
  • 1,187
  • 10
  • 26