C# WPF-在ItemsControl中以编程方式创建控件验证

C# WPF-在ItemsControl中以编程方式创建控件验证,c#,wpf,validation,xaml,combobox,C#,Wpf,Validation,Xaml,Combobox,我正在努力解决WPF代码中的奇怪问题。我有一个组合框,允许用户选择其中一个选项。此组合框中的每一项都是某种string.Format模式。例如,当用户选择选项“Hello{0}world”时,我的代码会生成两个文本块,其中包含“Hello”和“world”以及它们之间的一个文本框,用户可以在其中提供输入 这是我的xaml: <ComboBox ItemsSource="{Binding PossiblePatternOptions}" DisplayMemberPath="Pattern"

我正在努力解决WPF代码中的奇怪问题。我有一个组合框,允许用户选择其中一个选项。此组合框中的每一项都是某种string.Format模式。例如,当用户选择选项“Hello{0}world”时,我的代码会生成两个文本块,其中包含“Hello”和“world”以及它们之间的一个文本框,用户可以在其中提供输入

这是我的xaml:

<ComboBox ItemsSource="{Binding PossiblePatternOptions}" DisplayMemberPath="Pattern" SelectedItem="{Binding SelectedPattern, ValidatesOnDataErrors=True}" Width="250" Margin="5,0,25,0"/>
<ItemsControl ItemsSource="{Binding SelectedPattern.GeneratedControls}"/>
以下是我如何在GenerateControl方法中创建新的文本框:

TextBoxPlaceholder实现IDataErrorInfo并提供错误输入的错误详细信息:

public class TextBoxPlaceholder : IDataErrorInfo
{
   public string Value {get;set;}

   public string this[string columnName]
   {
      get
      {
         switch (columnName)
         {
            case "Value":
            return string.IsNullOrEmpty(Value) ? "Error" : string.Empty;

            default:
            return string.Empty;
         }
      }
    }

   public string Error
   {
      get { throw new NotImplementedException(); }
   }
}
问题是,当我第一次从combobox中选择一个选项时,生成的文本框被正确地验证,并且它们周围有漂亮的红框,但是当我选择以前选择的选项时,不会发生验证,也不再有红框。我注意到,当我更改GeneratedControls属性中的代码以便它每次都重新创建集合时,它工作正常。这里可能有什么问题


我知道这可能解释得不好,如果有任何误解,我会澄清。

似乎您的Value属性在更改时不会触发更新,因此文本框的文本绑定无法对更改做出反应,也不会再次评估值。试着这样做:

// implmeent INotifyPropertyChanged
public class TextBoxPlaceholder : IDataErrorInfo, System.ComponentModel.INotifyPropertyChanged
{
    private string mValue;

    public string Value 
    {
        get{ return mValue; }
        // fire notification
        set{mValue = value;NotifyPropertyChanged("Value");}
    }

    public event PropertyChangedEventHandler PropertyChanged;

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

    // your code goes here

它没有帮助,我认为通知机制只在您从代码更新属性时使用。我注意到一件奇怪的事。当我在文本框中输入有效输入并将其删除(即再次使其无效)时,会出现红色框。有什么想法吗?你的绑定和错误处理在我看来很好,我唯一想到的是:当你从组合框中选择一个新条目时,你确定你的Value属性被清除了吗?似乎您的值仅在开始时和手动清除时才被清除,然后错误验证才起作用,但在选择新条目时则不起作用。当我选择新条目时,将创建控件并使其起作用。当我第二次选择它时,验证中断。在键入正确的输入并再次使其无效后,它将再次开始工作。在任何情况下,都会激发PropertyChanged事件并正确设置Value属性。我怀疑这可能是ItemsControl本身的问题。现在,每次选择combobox条目时,我都会创建控件,不要更改它们。如果我有更多的时间,我会进一步调查。这听起来是一个很好的解决办法,如果你发现了问题,请告诉我,我很好奇可能是什么问题。
public class TextBoxPlaceholder : IDataErrorInfo
{
   public string Value {get;set;}

   public string this[string columnName]
   {
      get
      {
         switch (columnName)
         {
            case "Value":
            return string.IsNullOrEmpty(Value) ? "Error" : string.Empty;

            default:
            return string.Empty;
         }
      }
    }

   public string Error
   {
      get { throw new NotImplementedException(); }
   }
}
// implmeent INotifyPropertyChanged
public class TextBoxPlaceholder : IDataErrorInfo, System.ComponentModel.INotifyPropertyChanged
{
    private string mValue;

    public string Value 
    {
        get{ return mValue; }
        // fire notification
        set{mValue = value;NotifyPropertyChanged("Value");}
    }

    public event PropertyChangedEventHandler PropertyChanged;

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

    // your code goes here