简体   繁体   English

如何通过手动更新来处理可观察的集合绑定的数据网格

[英]How to cope with a observable collection binded datagrid with manual update

I have a datagrid 我有一个数据网格

<DataGrid Name="dtgFeatures" Height="100" Margin="10" ColumnWidth="*" CanUserAddRows="True" MouseLeftButtonUp="DtgFeatures_MouseLeftButtonUp"/>

which is binded to an observable collection 绑定到可观察的集合

ObservableCollection<CfgPartPrograms> obcCfgPartPrograms = new ObservableCollection<CfgPartPrograms>();

with

[Serializable]
public class CfgPartPrograms
{
    public CfgPartPrograms() { }
    public string Group{ get; set;}
    public string Description{ get; set;}
    public string Filename{ get; set;}<------set with openfiledialog
    public string Notes{ get; set;}
}

Now since I want to be able to insert the filename with an openfileDialog I have add this code: 现在,由于我希望能够使用openfileDialog插入文件名,因此添加了以下代码:

private void DtgFeatures_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    int column = (sender as DataGrid).CurrentCell.Column.DisplayIndex;
    if ( column == 2)
    {
        OpenFileDialog ofdPP = new OpenFileDialog();
        if (ofdPP.ShowDialog() == true)
        {
            if (obcCfgPartPrograms.Count == 0)
                obcCfgPartPrograms.Add(new CfgPartPrograms() { Filename = ofdPP.FileName });
            else
                obcCfgPartPrograms[selectedIndex].Filename = ofdPP.FileName;
            dtgFeatures.ItemsSource = null;
            dtgFeatures.ItemsSource = obcCfgPartPrograms;
        }
    }

the problem is that when I set the filename the observable collection has not been updated yet. 问题是,当我设置文件名时,可观察的集合尚未更新。 I'll explain that with images: 我将用图片进行说明:

在此处输入图片说明

So I have added aaaa and bbb now I want to force the filename with the code above but when I do that the bind action has not been done yet on the observable collection so that aaaa and bbbb are not present. 因此,我现在添加了aaa和bbb,我想使用上面的代码强制使用文件名,但是当我这样做时,可观察的集合还没有执行bind操作,因此aaa和bbbb不存在。

在此处输入图片说明

In short how to tell the binded datagrid to update the binding?? 简而言之,如何告诉绑定的数据网格更新绑定?

Thanks in advance Patrick 在此先感谢Patrick

Your CfgPartPrograms class should implement the INotifyPropertyChanged interface and raise the PropertyChanged event whenever a data bound property is set to a new value: https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx 每当数据绑定属性设置为新值时,您的CfgPartPrograms类都应实现INotifyPropertyChanged接口并引发PropertyChanged事件: https ://msdn.microsoft.com/zh-cn/library/system.componentmodel.inotifypropertychanged(v=vs .110).aspx

[Serializable]
public class CfgPartPrograms : System.ComponentModel.INotifyPropertyChanged
{
    public CfgPartPrograms() { }

    public string Group { get; set; }
    public string Description { get; set; }
    private string _fileName;

    public string Filename
    {
        get { return _fileName; }
        set { _fileName = value; NotifyPropertyChanged(); }
    }

    public string Notes { get; set; }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
    }
}

I found it here : I was missing the 我在这里找到它:我错过了

dtgFeatures.CommitEdit(DataGridEditingUnit.Row, true);

command 命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何处理可观察的类集合 - How to cope with an Observable collection of classes 如何在 C# WPF 中创建在运行时绑定到动态属性类型的可观察集合的 DataGrid 动态列 - How to create in C# WPF a DataGrid dynamic columns binded to an observable collection of dynamic properties types at runtime 检查绑定到可观察集合的数据网格中的单元格值是否为空或重复 - Check if a cell value in a datagrid binded to an observable collection is empty or duplicated ComboBox绑定到可观察集合如何添加1额外值 - ComboBox binded to observable collection how to add 1 extra value 如何以编程方式形成一个可观察的集合并将其绑定到数据网格 - How to programmatically form an observable collection and bind it to a datagrid C#WPF如何以编程方式更新数据网格后如何更新可观察的集合-无MVVM - C# WPF how to update observable collection after having programmatically updated datagrid - No MVVM 将可观察的集合绑定到数据网格? - Binding an observable collection to a datagrid? 数据网格和可观察的集合 - Datagrid and observable collection 如何更新与string [] []数组绑定的DataGrid中的单元格 - how to update a cell in DataGrid which is binded with string[][] array 当我更新GridView捆绑的集合时如何更新GridView? - How to update GridView when I update a collection that GridView is binded with?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM