绑定样式?
我在用户控件中有一些文本框:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我想要绑定到一个属性时,我也对写一些疯狂的长绑定短语每次感到非常恼火,所以我这样做了一年多,然后我偶然发现这篇文章。
它基本上是
MarkupExtension
的子类(这就是Binding
类是一个名为BindingDecoratorBase
的抽象类,提供 Binding 类提供的所有属性。因此,从那里你可以做这样的事情:然后你在 xaml 中所要做的就是在顶部包含你的名称空间,然后
要绑定到控件,请执行以下操作:
这比尝试对要在绑定短语中编写的每个控件进行子类化更容易。
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 aBinding
class is)to an abstract class calledBindingDecoratorBase
provides all of the properties that the Binding class provides. So from there you could do something like this: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:
This makes it easier than trying to subclass every single control that you want to write less in the binding phrase.
不,没有办法通过 XAML 或样式来执行此操作。您所能期望的最好结果就是构建一个可以更改默认行为的自定义控件。类似于:
那么您需要使用
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:
Then you'd need to use
MyTextBox
in place of theTextBox
.