如何WPF绑定书面内联=简短形式

PaN*_*1Me 7 wpf binding

我在表单中有大约100个TextBox.如果它们是十进制的,我需要验证它们.这有效,但它太冗长了,我不想在XAML中用800代替100行.

<TextBox.Text>
    <Binding Path="MyPath" UpdateSourceTrigger="PropertyChanged" Stringformat="{}{0:N}" NotifyOnValidationError="True">
        <Binding.ValidationRules>
            <myRulesNamespace:MyValidationRule ValidationType="decimal" />
        </Binding.ValidationRules>
    </Binding>
</TextBox.Text>
Run Code Online (Sandbox Code Playgroud)

有没有办法如何将它重写为这样的简短形式?:

Text="{Binding MyPath, UpdateSourceTrigger='PropertyChanged', StringFormat='{}{0:N}', NotifyOnValidationError=True, ValidationRules NOW WHAT?}"
Run Code Online (Sandbox Code Playgroud)

Gob*_*lin 6

简答:你做不到.Validation-rules属性是一个集合,目前无法在Binding简写中编写这些内容.

但是,您可以创建一个继承自Binding的类,如下所示:

public class SuperBinding:Binding
{
    public SuperBinding()
    {
        UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        ValidationRules.Add(new MyValidationRule{ValidationType = typeof(decimal)});
        //set rest of properties
    }
}
Run Code Online (Sandbox Code Playgroud)

然后使用它代替普通的Binding标记.