WPF绑定:禁用TextBoxes + Validation和DataContext问题

时间:2018-06-13 10:22:16

标签: c# wpf binding

我的简单程序有两个窗口:

  • 从第一个我设置Boolean值,然后......
  • 我将在第二个窗口中使用根据上述值本身禁用多个TextBox。

所述TextBox还具有验证绑定的特征。现在我的验证任务完美无缺,但是我无法对IsEnabled TextBox属性进行绑定。

这是我的XAML的片段,其中包含一个TextBoxes(现在是我唯一绑定的一个):

<TextBox x:Name="tbSlave1" Validation.Error="ValidationError" IsEnabled="{Binding TextBoxEnabled}" Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=SlavePoint1Name, ValidatesOnDataErrors=true, NotifyOnValidationError=true}"/>

虽然这是我的第二个窗口类:

public partial class GeneratorWindow : Window, INotifyPropertyChanged
{
    private readonly Validator validator = new Validator();

    private int noOfErrorsOnScreen;

    public GeneratorWindow()
    {
        this.InitializeComponent();
        this.grid.DataContext = this.validator;
    }

    public int NumberOfPoints { private get; set; }

    public int MainPDC { private get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    private Boolean IsEnabled;
    public Boolean TextBoxEnabled
    {
        get { return IsEnabled; }

        set
        {
            IsEnabled = value;
            NotifyPropertyChanged("TextBoxEnabled");
        }
    }

    private void ValidationError(object sender, ValidationErrorEventArgs eventArgs)
    {
        if (eventArgs.Action == ValidationErrorEventAction.Added)
        {
            this.noOfErrorsOnScreen++;
        }
        else
        {
            this.noOfErrorsOnScreen--;
        }
    }

    private void ValidationCanBeExecuted(object sender, CanExecuteRoutedEventArgs eventArgs)
    {
        eventArgs.CanExecute = this.noOfErrorsOnScreen == 0;
        eventArgs.Handled = true;
    }

    private void ValidationExecuted(object sender, ExecutedRoutedEventArgs eventArgs)
    {
        // If the validation was successful, let's generate the files.
        this.Close();

        eventArgs.Handled = true;
    }
}

现在,我回来的是我的窗口被禁用(无法选择任何TextBox),显然,这个:

  

System.Windows.Data错误:40:BindingExpression路径错误:&#39; TextBoxEnabled&#39;在&#39; object&#39;上找不到的属性&#39;&#39;验证&#39; (的HashCode = 14499481)&#39 ;. BindingExpression:路径= TextBoxEnabled;的DataItem =&#39;验证&#39; (的HashCode = 14499481);目标元素是&#39; TextBox&#39; (名称=&#39; tbSlave1&#39);目标财产是'IsEnabled&#39; (键入&#39;布尔&#39;)

根据我的理解,罪魁祸首就是我在我的类构造函数中管理DataContext的方式。我可能需要在验证器行中添加一些东西或完全改变它,但我无法理解。

1 个答案:

答案 0 :(得分:0)

如果您将DataContext设置为GeneratorWindow并相应地更新了绑定,我认为您应该没问题。为此,您需要将Validator更改为公共财产。

更改了验证码定义:

public Validator Validator { get; } = new Validator();

<强>的DataContext:

this.grid.DataContext = this;

更新了绑定:

<TextBox x:Name="tbSlave1" 
         Validation.Error="ValidationError" 
         IsEnabled="{Binding TextBoxEnabled}" 
         Text="{Binding UpdateSourceTrigger=PropertyChanged, 
                        Path=Validator.SlavePoint1Name, 
                        ValidatesOnDataErrors=true, 
                        NotifyOnValidationError=true}"/>