我的简单程序有两个窗口:
Boolean
值,然后...... 所述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
的方式。我可能需要在验证器行中添加一些东西或完全改变它,但我无法理解。
答案 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}"/>