【问题标题】:Restrict data in the ObservableCollection<T>限制 ObservableCollection<T> 中的数据
【发布时间】:2015-12-27 01:57:51
【问题描述】:

如果我的方法有问题,请告诉我。 我有一个包含数据网格的 WPF 窗口。这是为了让用户输入一个对象 ID 列表供程序处理。

我将 DataGrid 的 ItemsSource 绑定到 ObservableCollection,其中 MyObject 是具有单个字符串属性的类 - ObjectId。

我有一个关于何时更改收藏的事件:

 void TasksList_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            ProgressBarMax = TasksList.Count;
            TaskCountLabel = string.Format("{0} tasks to modify", TasksList.Count);
        }

我还想验证输入数据 - 用户可能提供了不正确的 ID,在这种情况下我不想将其添加到集合中。 但是,当我访问 e.NewItems[0] 对象时,它的 ObjectId 属性仍然为 null,因此无法验证。

我的方法有什么问题?

根据 cmets 添加数据网格 XAML:

<DataGrid Margin="5,0,5,10"
                                         ColumnWidth="*"
                                         ItemsSource="{Binding ElementName=ThisUc,
                                                               Path=TasksList,
                                                               Mode=TwoWay,
                                                               UpdateSourceTrigger=PropertyChanged}"
                                         Style="{x:Null}"
                                         CanUserAddRows="True" CanUserPasteToNewRows="True"
                                         x:Name="TasksDataGrid"
                                         CanUserDeleteRows="True"
                                         VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
                                         SelectionUnit="Cell" />

【问题讨论】:

  • 也许您需要在之前将它添加到集合中而不是之后执行此验证。您目前如何将项目添加到此集合中? ICommand?
  • @MikeEason - 不...集合绑定到数据网格,因此当用户开始在数据网格单元格中输入时它会更新...
  • 发布您的数据网格 xaml

标签: c# wpf data-binding datagrid observablecollection


【解决方案1】:

我还想验证输入数据

只需使用 WPF 验证功能。例如,基于IDataErrorInfo 的验证:

public class RowViewModel : INotifyPropertyChanged, IDataErrorInfo
{
    // INPC implementation is omitted

    public string Id
    {
        get { return id; }
        set
        {
            if (id != value)
            {
                id = value;
                OnPropertyChanged();
            }
        }
    }
    private string id;

    public string this[string columnName]
    {
        get
        {
            if (string.IsNullOrEmpty(Id))
            {
                return "Id cannot be an empty string.";
            }

            return null;
        }
    }

    public string Error
    {
        get { return null; }
    }
}

    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Rows}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Id" Binding="{Binding Id, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"/>
        </DataGrid.Columns>
    </DataGrid>

用户可能提供了不正确的 ID,在这种情况下我不想 将其添加到集合中

你不能。

DataGrid 使用就地编辑时,添加新行会自动将新项目添加到绑定ItemsSource。但是如果行数据源使用验证,用户不能提交更改,直到出现验证错误。

在这种情况下,用户有一种方法 - 取消编辑,当他取消时,DataGrid 会从底层 ItemsSource 中删除新行。

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 2011-06-13
    • 1970-01-01
    • 2011-09-09
    相关资源
    最近更新 更多