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

Jus*_*tin 56 c# data-binding dependency-properties

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

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

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

Die*_*hon 96

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

new FrameworkPropertyMetadata
{
    BindsTwoWayByDefault = true,
    DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
}
Run Code Online (Sandbox Code Playgroud)

  • 我可以通过将它添加到我的示例dp来设置BindsTwoWayByDefault:new FrameworkPropertyMetadata(0,FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,OnBindableSelectionStartChanged).但是,我仍然无法将UpdateSourceTrigger设置为PropertyChanged. (3认同)
  • 值得一提的是,除“ TwoWay”外,无法将默认值更改为其他任何模式。因此,排除了OneTime和OneWayToSource。至少我没有找到办法。 (2认同)

Pau*_*ich 18

在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); }
}
Run Code Online (Sandbox Code Playgroud)