Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF验证与测试;伊达埃罗林福酒店_Wpf_Validation_Idataerrorinfo - Fatal编程技术网

WPF验证与测试;伊达埃罗林福酒店

WPF验证与测试;伊达埃罗林福酒店,wpf,validation,idataerrorinfo,Wpf,Validation,Idataerrorinfo,注意-我拥有的类是EntityObject类 我有以下课程: public class Foo { public Bar Bar { get; set; } } public class Bar : IDataErrorInfo { public string Name { get; set; } #region IDataErrorInfo Members string IDataErrorInfo.Error { get { ret

注意-我拥有的类是
EntityObject

我有以下课程:

public class Foo
{
    public Bar Bar { get; set; }
}

public class Bar : IDataErrorInfo
{
    public string Name { get; set; }

    #region IDataErrorInfo Members
    string IDataErrorInfo.Error
    {
        get { return null; }
    }

    string IDataErrorInfo.this[string columnName]
    {
        get
        {
            if (columnName == "Name")
            {
                return "Hello error!";
            }
            Console.WriteLine("Validate: " + columnName);
            return null;
        }
    }
    #endregion
}
XAML如下所示:

<StackPanel Orientation="Horizontal" DataContext="{Binding Foo.Bar}">
     <TextBox Text="{Binding Path=Name, ValidatesOnDataErrors=true}"/>
</StackPanel>


我在那里的验证上放置了一个断点和一个
控制台。Writeline
——我没有中断。未执行验证。有人能把我按到错误所在的地方吗?

您忘了在“Bar”类上实现INotifyPropertyChanged,那么只有绑定系统才会触发setter

因此,您的“名称”属性很可能是

public string Name 
    { 
          get{ return _name; } 
      set
      {
          _name = value;
          RaisePropertyChanged("Name"); // Or the call might OnPropertyChanged("Name");
      }
    }

我不熟悉EntityObject类,在.NETFramework文档或快速谷歌搜索中也找不到它

总之,您需要做的是我们也使用:



您应该将实现IDataErrorInfo的方法公开。

尝试在绑定上设置Mode=TwoWay

这可能是一个愚蠢的答案,但默认情况下,当发生
LostFocus
时,绑定会调用setter。如果你还没有这样做,那就试试吧


如果希望在每次按键时触发错误代码,请在绑定中使用
UpdateSourceTrigger=PropertyChanged

应创建包含Bar类引用的本地窗口资源,并使用其键设置StackPanel数据上下文属性。另外,不要忘记在窗口或用户控件中导入其名称空间

您的XAML代码应该如下所示:

<Window x:Class="Project.WindowName"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:local="clr-namespace:BarNamespace">  
<Window.Resources>
 <local:Bar x:Key="bar" />
</Window.Resources>
<StackPanel Orientation="Horizontal" DataContext="{StaticResource bar}">
      <TextBox Text="{Binding Path=Name, ValidatesOnDataErrors=true}"/>
</StackPanel> 
</Window>


但正如我上面提到的-EntityObject类的Foo和Bar以及子类(它会通知OnPropertyChange)。我实际上认为EntityObject父类会以某种方式影响这一点,因为IDataErrorInfo通常工作正常。@Jefim:你说的父/子类是什么意思?您呈现的Foo和Bar没有继承任何内容。如果您的意思是将Foo和Bar声明为某个EntityObject派生类的内部类,那么这根本不会改变任何事情。在C#中,内部类只影响外部类成员的公共/受保护/私有可见性。它们对代码/事件/属性/继承/等行为没有影响;执行此操作时禁止使用可见性修饰符。并且,在使用显式接口实现时,根本不需要使用
public
。这仍然是接口的有效实现,接口客户端将看到它。我认为这是WPF对
TextBox.Text
denendency属性的默认设置,因此在这里没有关系。
<Window x:Class="Project.WindowName"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:local="clr-namespace:BarNamespace">  
<Window.Resources>
 <local:Bar x:Key="bar" />
</Window.Resources>
<StackPanel Orientation="Horizontal" DataContext="{StaticResource bar}">
      <TextBox Text="{Binding Path=Name, ValidatesOnDataErrors=true}"/>
</StackPanel> 
</Window>