绑定WPF DependencyProperty的默认设置

时间:2010-02-16 10:25:34

标签: c# wpf xaml dependency-properties

我创建了一个名为MyCustomComboBox的自定义用户控件。我在应用程序中的任何地方都做了以下内容:

    <Widgets:MyCustomComboBox
        Foo="{Binding Foo, 
            UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" /> 

MyCustomComboxBox具有依赖属性Foo,我在组合框中有一些验证和其他逻辑,这就是我将它包装在自定义控件中的原因。

自定义组合框包含另一个用户控件,该控件也具有Foo属性,组合框绑定到该属性。

但我还必须设置UpdateSourceTriggerMode,我想以某种方式指定那些是绑定到DependencyProperty时的默认值。可以吗?

1 个答案:

答案 0 :(得分:3)

可以在依赖项属性元数据中指定默认BindingMode

public static readonly DependencyProperty FooProperty = DependencyProperty.Register(
    "Foo",
    typeof(string),
    typeof(MyCustomComboBox),
    new FrameworkPropertyMetadata(
        null,
        FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);

但是,据我所知,无法为更新源触发器提供默认值。

相关问题