如何让 ObservableCollection 与 IDataError 一起使用
我有一个对话框,显示 ObservableCollection(整数的集合)的各个元素。当用户更改集合的各个值时,我想验证更改。
在我的 VM 中,只有一个集合属性,而我的 XAML 绑定到各个元素。
那么,当集合中的项目发生更改时,如何调用验证属性索引器呢?
我对 WPF 还很陌生,所以我想有一个简单的解决方案。
感谢您的帮助。
这是 XAML。 10 个通道中的每一个通道都有一个等效的文本框
<TextBox Grid.Row="0" Grid.Column="1" Name="chan01"
HorizontalAlignment="Left" Width="60"
Text="{Binding ChannelList[0].ChannelNumber, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged,
NotifyOnValidationError=True}" />
以下是我的虚拟机中隐藏的一些代码。 grp_对象是模型
/// <summary>
/// Scan channel list.
/// </summary>
public ChannelNumberCollection ChannelList
{
get
{
return grp_.ChannelList;
}
set
{
grp_.ChannelList = value; NotifyPropertyChanged("ChannelList");
}
}
public string this[string propertyName]
{
get
{
switch (propertyName)
{...
I have dialog box that displays individual elements of an ObservableCollection(a collection o ints). As the user changes individual values of the collection, I want to validate the change.
In my VM, there is only a property for the collection, and my XAML is bound to the individual elements.
So how do I get the validation property indexer to be called when an item in the collection changes?
I'm still pretty new to WPF so I image there's a simple solution to this..
Thanks for you help.
Here's the XAML. There's an equivalent text box for each of the 10 channels
<TextBox Grid.Row="0" Grid.Column="1" Name="chan01"
HorizontalAlignment="Left" Width="60"
Text="{Binding ChannelList[0].ChannelNumber, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged,
NotifyOnValidationError=True}" />
Here's some of the code behind in my VM. The grp_ object is the Model
/// <summary>
/// Scan channel list.
/// </summary>
public ChannelNumberCollection ChannelList
{
get
{
return grp_.ChannelList;
}
set
{
grp_.ChannelList = value; NotifyPropertyChanged("ChannelList");
}
}
public string this[string propertyName]
{
get
{
switch (propertyName)
{...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的方法不会起作用,因为为了使 ValidatesOnDataError 属性起作用,Binding 的目标必须实现 IDataErrorInfo。在您的场景中,ChannelNumber 属性是目标,而不是 ChannelNumberCollection 本身,并且您的代码片段表明该集合实现了 IDataErrorInfo。
我认为您需要:
或
I don't think your approach will work because in order for the ValidatesOnDataError property to work, the target of the Binding must implement IDataErrorInfo. In your scenario, the ChannelNumber property is the target, not the ChannelNumberCollection itself and your code snippet indicates that the collection implements IDataErrorInfo.
I think you either need to:
or