绑定风格?

Dum*_*y01 4 .net wpf xaml

我在用户控件中有一些文本框:

<TextBox Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<TextBox Text="{Binding Path=Street, UpdateSourceTrigger=PropertyChanged}"></TextBox>
Run Code Online (Sandbox Code Playgroud)

在XAML中是否有一种方法可以为我的绑定做一些样式,这样我就不必为每个文本框写入UpdateSourceTrigger=PropertyChanged但只有Path=部分?

先感谢您!

Jos*_*ose 5

在写一些疯狂的长期结合词组我也得到了真正恼火EVERY时间,我想绑定到一个属性,所以我做了一年多前,我偶然发现这个职位.

它基本上是一个抽象类的子类MarkupExtension(这是一个Binding类),它BindingDecoratorBase提供了Binding类提供的所有属性.所以从那里你可以做这样的事情:

public class SimpleBinding : BindingDecoratorBase
{
  public SimpleBinding(string path) : this()
  {
    Path = new System.Windows.PropertyPath(path);
  }
  public SimpleBinding()
  {
    TargetNullValue = string.Empty;
    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
  }
}
Run Code Online (Sandbox Code Playgroud)

然后你需要在你的xaml中做的就是在顶部包含你的命名空间然后绑定到一个控件做类似这样的事情:

<TextBox Text="{m:SimpleBinding Name}"></TextBox>
<TextBox Text="{m:SimpleBinding Street}"></TextBox>
Run Code Online (Sandbox Code Playgroud)

这使得比尝试将每个要在绑定短语中少写的控件子类化更容易.