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

时间:2016-02-26 14:07:27

标签: c# wpf observablecollection objectlistview

我已经更新了列表的列表项。该项目已在源数据库中成功更新,但列表未使用更新的项目进行更新。我已使用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 ?
    }

https://stackoverflow.com/a/13500078/374198

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

提前致谢

通过Linq到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));
        }
    }
}

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>

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

这是我做错的地方 - :      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;

正确的方式:

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


   on clicking edit

   ModelItem = SelectedModelItem;

4 个答案:

答案 0 :(得分:1)

你真的确定你从 WITHIN 你的收藏中更新了一个ModelItem吗?你没有发布代码。

绑定和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:并从

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

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

答案 1 :(得分:0)

ModelItem的属性也需要实现INotifyPropertyChanged

答案 2 :(得分:0)

您必须在模型中实现INotifyPropertyChanged。

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

Model或ViewModel的每个属性都需要更改属性。

答案 3 :(得分:0)

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));
      }
    }
}