2015-03-02 66 views
1

我在我的CustomControl上設置了一個BindingGroup並實現了一個Validator函數。以下是來自XAML的代碼片段。我的問題是驗證器永遠不會被調用。當我創建一個小樣本程序時,事情正在起作用。我怎麼能找出原因呢?Bindinggroup valiation function not called

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

}

+0

郵政更多的代碼... – 2015-03-02 17:54:01

+0

我已經減少了該項目只是一個XAML和代碼背後,仍然顯示錯誤。 – 2015-03-02 19:30:52

回答

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