我正在涉足 WPF,并注意到一个我以前从未见过的特性。在下面的示例中,我有两个文本框绑定到代码隐藏中的同一个 DP。
代码隐藏:
public partial class MainWindow : Window
{
    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(Window), new FrameworkPropertyMetadata("Hello"));
    public MainWindow()
    {
        InitializeComponent();
    }
}
和 XAML:
    <TextBox>
        <TextBox.Text>
            <Binding RelativeSource = "{RelativeSource Mode=FindAncestor, AncestorType=Window}" Path="Text" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay">
                <Binding.ValidationRules>
                    <utils:RestrictInputValidator Restriction="IntegersOnly" ValidatesOnTargetUpdated="True"/>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>
    <TextBox Name="TextBox" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Mode=TwoWay, Path=Text, UpdateSourceTrigger=PropertyChanged}"/>
我注意到,当我在 TextBox 中键入包含 IntegerOnly 验证的内容时,验证失败(在这种情况下,任何不是整数的内容),底层 Text 变量都不会更新。这是默认行为吗?为什么这样做?是否可以覆盖?
