未调用绑定组验证功能

时间:2015-03-02 17:50:09

标签: wpf xaml

我在CustomControl上设置了BindingGroup并实现了Validator功能。以下是XAML的代码片段。我的问题是验证器永远不会被调用。当我创建一个小样本程序时,事情正在发挥作用。我怎么能追查原因?

XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
<Window.BindingGroup>
    <BindingGroup>
        <BindingGroup.ValidationRules>
            <local:DurationValidator/>
        </BindingGroup.ValidationRules>
    </BindingGroup>
</Window.BindingGroup>
<StackPanel>
    <TextBox Text="{Binding SomeString, UpdateSourceTrigger=PropertyChanged}"></TextBox>
    <Button>add</Button>
</StackPanel>
</Window>

代码背后:

namespace WpfApplication1
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        SomeString = "Some string";
    }

    private string _someString;
    public string SomeString
    {
        get { return _someString; }
        set
        {
            if (_someString == value) return;
            _someString = value;
            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("SomeString"));
        }
    }
}
public class DurationValidator : ValidationRule
{
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        BindingGroup bindingGroup = (BindingGroup)value;
        return new ValidationResult(false, "Whatever");
    }
}

}

1 个答案:

答案 0 :(得分:2)

请参阅以下代码。

XAML

 <StackPanel x:Name="stk">
    <StackPanel.BindingGroup>
        <BindingGroup Name="myBindingGroup">
            <BindingGroup.ValidationRules>
                <local:DurationValidator ValidatesOnTargetUpdated="True" />
            </BindingGroup.ValidationRules>
        </BindingGroup>
    </StackPanel.BindingGroup>
    <TextBox Text="{Binding BindingGroupName=myBindingGroup,UpdateSourceTrigger=PropertyChanged,Path=SomeString,Mode=TwoWay}"></TextBox>
    <Button>add</Button> 
</StackPanel> 

C#

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        SomeString = "Some string";
    }

    private string _someString;
    public string SomeString
    {
        get { return _someString; }
        set
        {
            if (_someString == value) return;
            _someString = value;
            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("SomeString"));
            this.stk.BindingGroup.CommitEdit();
        }
    }
}
public class DurationValidator : ValidationRule
{
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        BindingGroup bindingGroup = (BindingGroup)value;
        return new ValidationResult(false, "Whatever");
    }
}