绑定样式?

发布于 2024-11-02 09:37:41 字数 385 浏览 0 评论 0原文

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

<TextBox Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<TextBox Text="{Binding Path=Street, UpdateSourceTrigger=PropertyChanged}"></TextBox>

在 XAML 中是否有一种方法可以为我的绑定执行类似样式的操作,以便我不必为每个文本框编写 UpdateSourceTrigger=PropertyChanged 但只有 Path= 部分?

先感谢您!

I have some text boxes in a user control:

<TextBox Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<TextBox Text="{Binding Path=Street, UpdateSourceTrigger=PropertyChanged}"></TextBox>

Is there a way in XAML to do something like a style for my bindings so that I don't have to write for each text box the UpdateSourceTrigger=PropertyChanged but only the Path= part?

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

擦肩而过的背影 2024-11-09 09:37:41

当我想要绑定到一个属性时,我也对写一些疯狂的长绑定短语每次感到非常恼火,所以我这样做了一年多,然后我偶然发现这篇文章。

它基本上是 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;
  }
}

然后你在 xaml 中所要做的就是在顶部包含你的名称空间,然后
要绑定到控件,请执行以下操作:

<TextBox Text="{m:SimpleBinding Name}"></TextBox>
<TextBox Text="{m:SimpleBinding Street}"></TextBox>

这比尝试对要在绑定短语中编写的每个控件进行子类化更容易。

I too got real annoyed at writing some crazy long binding phrase EVERY time I wanted to bind to a property, so I did that for over a year before I stumbled across this post.

It basically subclasses MarkupExtension (which is what a Binding class is)to an abstract class called BindingDecoratorBase provides all of the properties that the Binding class provides. So from there you could do something like this:

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

And then all you have to do in your xaml is include your namespace at the top and then
to bind to a control do something like this:

<TextBox Text="{m:SimpleBinding Name}"></TextBox>
<TextBox Text="{m:SimpleBinding Street}"></TextBox>

This makes it easier than trying to subclass every single control that you want to write less in the binding phrase.

jJeQQOZ5 2024-11-09 09:37:41

不,没有办法通过 XAML 或样式来执行此操作。您所能期望的最好结果就是构建一个可以更改默认行为的自定义控件。类似于:

public class MyTextBox : TextBox {
    static MyTextBox() {
        TextProperty.OverrideMetadata(typeof(MyTextBox), new FrameworkPropertyMetadata() { DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
    }
}

那么您需要使用 MyTextBox 代替 TextBox

No, there isn't a way to do this via XAML or a Style. The best you can hope for is to build a custom control that changes the default behavior. Something like:

public class MyTextBox : TextBox {
    static MyTextBox() {
        TextProperty.OverrideMetadata(typeof(MyTextBox), new FrameworkPropertyMetadata() { DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
    }
}

Then you'd need to use MyTextBox in place of the TextBox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文