风格对于绑定?绑定、风格

2023-09-05 00:25:37 作者:称霸幼儿园!

我有一个用户控件的一些文本框:

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>

有没有办法在XAML中做这样的事情对我的绑定风格,这样我就不必写每一个文本框的 UpdateSourceTrigger =的PropertyChanged 但只在路径= 部分?

感谢你在前进!

推荐答案

没有,没有办法通过XAML或风格来做到这一点。你可以期望的最好的是建立,改变默认行为自定义控件。是这样的:

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

然后,你需要使用 MyTextBox 代替文本框