检查 ObservableCollection 是否有效

发布于 2024-08-16 04:43:00 字数 282 浏览 1 评论 0原文

我有一个 WPF Dev Express DxGrid,它通过以下方式绑定到 ObservableCollection。

Private _FamilyList As New ObservableCollection(Of FamilyRecord)
MyGrid.DataSource = _FamilyList

当用户开始在网格中输入信息时,我需要能够检查他们是否错过了某些信息,从而使其无效。

那么,检查 _FamilyList 没有验证错误的最佳方法是什么?

I have a WPF Dev Express DxGrid that is bound to an ObservableCollection in the following way.

Private _FamilyList As New ObservableCollection(Of FamilyRecord)
MyGrid.DataSource = _FamilyList

When a user starts to enter information in the grid, I need to be able to check whether they have missed some information making it Invalid.

So, what is the best method of checking that the _FamilyList has no validation errors?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里寻她 2024-08-23 04:43:00

我没有 DevExpress 网格的经验,但在 Xceed WPF DataGridControl 上有一个名为 UpdateSourceTrigger 的属性,它控制数据源何时更新(当用户完成编辑整行时,完成)编辑单元格,或每次击键)。我确信 DevExpress 也有类似的概念。

这将使您能够控制验证发生的时间。您可以将数据验证逻辑放在 FamilyRecord 类本身中。当您检测到错误时,将 FamilyRecord 置于错误状态,该状态将在网格中提供视觉提示。

编辑:

要在保存时确定集合中的任何 FamilyRecord 对象是否有任何错误,您需要如下所示:

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {

        ObservableCollection<FamilyRecord> _familyRecords;

        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _familyRecords = new ObservableCollection<FamilyRecord>();
            _familyRecords.Add(new FamilyRecord(@"Jones", false));
            _familyRecords.Add(new FamilyRecord(@"Smith", true));

            comboBox1.ItemsSource = _familyRecords;
        }

        // save button
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // validate whether any records still have errors before we save.
            if (_familyRecords.Any(f => f.HasErrors))
            {
                MessageBox.Show(@"Please correct your errors!");
            }
        }


    }

    public class FamilyRecord
    {

        public FamilyRecord(string name, bool hasErrors)
        {
            Name = name;
            HasErrors = hasErrors;
        }

        public string Name { get; set; }
        public bool HasErrors { get; set; }

        public override string ToString()
        {
            return this.Name;
        }
    }
}

I don't have experience w/ the DevExpress grid, but on the Xceed WPF DataGridControl there's a property called UpdateSourceTrigger which controls when the data source gets updated (when the user is finished editing an entire row, finished editing a cell, or with every key stroke). I'm sure DevExpress has a similar concept.

This will give you control over when the validation happens. You can put your data validation logic in the FamilyRecord class itself. When you detect an error put the FamilyRecord into an error state that will provide a visual cue in the grid.

EDIT:

To determine, upon saving, whether any FamilyRecord objects in your collection have any errors you'd want something like this:

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {

        ObservableCollection<FamilyRecord> _familyRecords;

        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _familyRecords = new ObservableCollection<FamilyRecord>();
            _familyRecords.Add(new FamilyRecord(@"Jones", false));
            _familyRecords.Add(new FamilyRecord(@"Smith", true));

            comboBox1.ItemsSource = _familyRecords;
        }

        // save button
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // validate whether any records still have errors before we save.
            if (_familyRecords.Any(f => f.HasErrors))
            {
                MessageBox.Show(@"Please correct your errors!");
            }
        }


    }

    public class FamilyRecord
    {

        public FamilyRecord(string name, bool hasErrors)
        {
            Name = name;
            HasErrors = hasErrors;
        }

        public string Name { get; set; }
        public bool HasErrors { get; set; }

        public override string ToString()
        {
            return this.Name;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文