.net WPF 控件的 Binding.Mode=Default 的默认值是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1797105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:35:27  来源:igfitidea点击:

What are the defaults for Binding.Mode=Default for WPF controls?

.netwpfbindingdependency-propertiesbinding-mode

提问by Shimmy Weitzhandler

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

在WPF中Binding.Mode,在选择默认值时,它取决于属性绑定。

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

我正在寻找一些列表或一些约定或各种控件的默认值的任何信息。
我的意思是TwoWay,默认情况下是什么属性等等。欢迎任何链接、想法、想法甚至咆哮!

回答by Lars Truijens

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 TwoWayas 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 BindsTwoWayByDefaultis 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 GetMetadataand then check the boolean value of the BindsTwoWayByDefaultproperty.

与 UpdateSourceTrigger 类似,Mode 属性的默认值因每个属性而异。用户可编辑的属性,如 TextBox.TextComboBox.TextMenuItem.IsChecked,等,都有TwoWay它们的默认模式的价值。要确定默认值是否为TwoWay,请查看该属性的 Dependency Property Information 部分。如果它说 BindsTwoWayByDefault设置为 true,则该属性的默认 Mode 值为TwoWay。要以编程方式执行此操作,请通过调用获取属性的属性元数据, GetMetadata然后检查BindsTwoWayByDefault属性的布尔值。

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

来源: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.

最安全的方法是始终明确您希望从绑定中获得哪种绑定模式。

回答by akjoshi

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

这是一种查找 DP 支持的默认模式的方法 -

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

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.

.NET Reflector 是您的朋友。使用反射器,搜索TextBox并查看静态构造函数 ( .cctor())的源代码。在这里,您将能够找到用于注册TextPropertyDP的代码:

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
                   )
                );

请注意,向 Register 方法传递了一个参数,指示默认绑定模式: FrameworkPropertyMetadataOptions.BindsTwoWayByDefault。如果您使用反射器查看 TextBlock 的 Text DP 的注册,您将看到没有传递这样的值,在这种情况下,我们假设绑定是默认的一种方式。

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

摘自 Bea Stollnitz 的帖子:如何更新模板中的显式绑定?

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

尽管拥有某种重要 DP 的列表会非常有帮助。

回答by Freek Sanders

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.

有趣的是,大多数控件都有一个 TextSearch 属性,它具有双向绑定。