有没有办法指定自定义依赖项属性默认绑定模式和更新触发器?

时间:2010-04-18 21:05:37

标签: c# data-binding dependency-properties

我想这样做,默认情况下,当我绑定到我的一个依赖项属性时,绑定模式是双向的,并且update-trigger属性已更改。有没有办法做到这一点?

以下是我的一个依赖项属性的示例:

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

2 个答案:

答案 0 :(得分:92)

注册属性时,请使用以下命令初始化元数据:

new FrameworkPropertyMetadata
{
    BindsTwoWayByDefault = true,
    DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
}

答案 1 :(得分:16)

在Dependency Property声明中,它看起来像这样:

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