WPF-如何更新列表的列表项中的更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35653840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:41:54  来源:igfitidea点击:

WPF- How to update the changes in list item of a list

c#wpfobservablecollectionobjectlistview

提问by Ivan D'souza

i have updated a list item of a list.The item is successfully updated at source i.e database ,but the list is not getting updated with updated item.I have used INotifyPropertyChanged interface for the list items and the list is binded to an observable collection.

我已经更新了列表的列表项。该项目已在源即数据库中成功更新,但该列表没有使用更新的项目进行更新。我已将 INotifyPropertyChanged 接口用于列表项,并且该列表已绑定到一个可观察的集合。

    private tbl_Model _modelItem;
    public tbl_Model ModelItem
    {
        get { return _modelItem; }
        private set
        {
            _modelItem = value;
            NotifyPropertyChanged("ModelItem");
        }
    }

    private ObservableCollection<tbl_Model> _modelCollection;
    public ObservableCollection<tbl_Model> ModelCollection
    {
        get { return _modelCollection; }
        private set
        {
            _modelCollection = value;
            NotifyPropertyChanged("ModelCollection");
        }
    }

    public void btn_update()
    {
       //Code to update at database 
       //what should i write here to update the list ?
    }

This is the image of the window of the list

这是列表窗口的图像

As you can see in image,the list shows model no. as 101 even after i updated it to 102

正如您在图片中看到的,列表显示了型号。即使在我将其更新为 102 后仍为 101

Thanks in advance

提前致谢

The auto generated model via Linq to Sql

通过 Linq to Sql 自动生成的模型

 public partial class tbl_Model : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _ID;

    private string _Model_No;

    private string _Name;

    private string _Manufacturer;

    private int _IsDelete;

#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnIDChanging(int value);
partial void OnIDChanged();
partial void OnModel_NoChanging(string value);
partial void OnModel_NoChanged();
partial void OnNameChanging(string value);
partial void OnNameChanged();
partial void OnManufacturerChanging(string value);
partial void OnManufacturerChanged();
partial void OnIsDeleteChanging(int value);
partial void OnIsDeleteChanged();
#endregion

    public tbl_Model()
    {
        OnCreated();
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
    public int ID
    {
        get
        {
            return this._ID;
        }
        set
        {
            if ((this._ID != value))
            {
                this.OnIDChanging(value);
                this.SendPropertyChanging();
                this._ID = value;
                this.SendPropertyChanged("ID");
                this.OnIDChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Model_No", DbType="VarChar(50) NOT NULL", CanBeNull=false)]
    public string Model_No
    {
        get
        {
            return this._Model_No;
        }
        set
        {
            if ((this._Model_No != value))
            {
                this.OnModel_NoChanging(value);
                this.SendPropertyChanging();
                this._Model_No = value;
                this.SendPropertyChanged("Model_No");
                this.OnModel_NoChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Name", DbType="VarChar(50) NOT NULL", CanBeNull=false)]
    public string Name
    {
        get
        {
            return this._Name;
        }
        set
        {
            if ((this._Name != value))
            {
                this.OnNameChanging(value);
                this.SendPropertyChanging();
                this._Name = value;
                this.SendPropertyChanged("Name");
                this.OnNameChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Manufacturer", DbType="VarChar(50) NOT NULL", CanBeNull=false)]
    public string Manufacturer
    {
        get
        {
            return this._Manufacturer;
        }
        set
        {
            if ((this._Manufacturer != value))
            {
                this.OnManufacturerChanging(value);
                this.SendPropertyChanging();
                this._Manufacturer = value;
                this.SendPropertyChanged("Manufacturer");
                this.OnManufacturerChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_IsDelete", DbType="Int NOT NULL")]
    public int IsDelete
    {
        get
        {
            return this._IsDelete;
        }
        set
        {
            if ((this._IsDelete != value))
            {
                this.OnIsDeleteChanging(value);
                this.SendPropertyChanging();
                this._IsDelete = value;
                this.SendPropertyChanged("IsDelete");
                this.OnIsDeleteChanged();
            }
        }
    }

    public event PropertyChangingEventHandler PropertyChanging;

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void SendPropertyChanging()
    {
        if ((this.PropertyChanging != null))
        {
            this.PropertyChanging(this, emptyChangingEventArgs);
        }
    }

    protected virtual void SendPropertyChanged(String propertyName)
    {
        if ((this.PropertyChanged != null))
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

The ListView XAML Code

ListView XAML 代码

<ListView ItemsSource="{Binding ModelCollection,Mode=TwoWay}" SelectedItem="{Binding SelectedModelItem}" Style="{StaticResource viewinglist}">                      
                    <ListView.View>
                        <GridView>
                            <GridViewColumn DisplayMemberBinding="{Binding Model_No, Mode=TwoWay}" Header="Model No." Width="100"/>
                            <GridViewColumn DisplayMemberBinding="{Binding Name, Mode=TwoWay}" Header="Model Name"  Width="200"/>
                            <GridViewColumn DisplayMemberBinding="{Binding Manufacturer, Mode=TwoWay}" Header="Manufacturer"  Width="200"/>
                        </GridView>
                    </ListView.View>
                </ListView>

Solution for anyone who sees the post:

任何看到帖子的人的解决方案:

This is where i was doing wrong-: public tbl_Model SelectedModelItem {get; set;}

这是我做错的地方-:public tbl_Model SelectedModelItem {get; 放;}

 //on clicking edit this is what i used to do
  ModelItem.ID = SelectedModelItem.ID;
  ModelItem.Model_No = SelectedModelItem.Model_No;
  ModelItem.Name = SelectedModelItem.Name;
  ModelItem.Manufacturer = SelectedModelItem.Manufacturer;

The right way :

正确的方式 :

  private tbl_Model _selectedModelItem;
    public tbl_Model SelectedModelItem 
    {
        get { return _selectedModelItem; }
        set
        {
            _selectedModelItem = value;
            NotifyPropertyChanged("SelectedModelItem");
        } 
    }


   on clicking edit

   ModelItem = SelectedModelItem;

采纳答案by blindmeis

are you really sure that you update a ModelItem from WITHINyour Collection? you did not post the code.

你真的确定你从更新ModelItem WITHIN您的收藏?你没有发布代码。

The bindings and INotifyPropertyChanged implementation looks well.

绑定和 INotifyPropertyChanged 实现看起来不错。

<ListView ItemsSource="{Binding ModelCollection,Mode=TwoWay}" 
          SelectedItem="{Binding SelectedModelItem}" />


private tbl_Model _modelItem;
public tbl_Model SelectedModelItem 
{
    get { return _modelItem; }
    private set
    {
        _modelItem = value;
        NotifyPropertyChanged("SelectedModelItem");
    }
}

public void Update()
{
   SelectedModelItem.Model_No = "102";//Ui get notified, cause its a ModelItem from your Collection
}

ps: and pls remove the TwoWay Binding from

ps:并请删除双向绑定

<ListView ItemsSource="{Binding ModelCollection,Mode=TwoWay}"

your ListeView will never set a ModelCollection back to your Viewmodel.

您的 ListeView 永远不会将 ModelCollection 设置回您的 Viewmodel。

回答by Marc

The properties of ModelItemalso needs to implement INotifyPropertyChanged.

的属性ModelItem也需要实现INotifyPropertyChanged

回答by Nawed Nabi Zada

You have to implement INotifyPropertyChanged in your model.

您必须在模型中实现 INotifyPropertyChanged。

In case your model is auto generated, then you can create a ViewModel for your model which implements INotifyPropertyChanged.

如果您的模型是自动生成的,那么您可以为实现 INotifyPropertyChanged 的​​模型创建一个 ViewModel。

Each property of your Model or ViewModel needs to yield property changed.

您的 Model 或 ViewModel 的每个属性都需要生成已更改的属性。

回答by abhilash

The ObservableCollectionraises events automatically but for ModelItem's properties you have to raise the events yourself.

ObservableCollection自动引发事件但对ModelItem的属性,你必须提高自己的事件。

public class ModelItem : INotifyPropertyChanged
{

  private int modelNumber;
  public int ModelNumber
  {
    get { return modelNumber; }
    set 
    {

      modelNumber = value; 
      NotifyPropertyChanged("ModelNumber"); }
    }

  //Similar implementation for other Properties Model Name, Manufacturer

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
      PropertyChangedEventHandler handler = PropertyChanged;
      if (null != handler)
      {
        handler(this, new PropertyChangedEventArgs(propertyName));
      }
    }
}