简体   繁体   中英

Bind Listview to ObservableCollection

I am trying to bind a listview to an ObservableCollection.

I took a look at several topics on stack, but cannot find what I am doing wrong :

My XAML looks like that (for GridViewColumn I tried both wih Path=, and without, as I saw both of them) :

<ListView x:Name="ListView1"  ItemsSource="{Binding listeAff}"  Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="4" MouseDoubleClick="ListView_MouseDoubleClick" GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler" >
            <ListView.ContextMenu>
                <ContextMenu x:Name="Context1">
                    <MenuItem Header="{x:Static p:Resources.ContextSupprimer}" Click="Supprimer_Element"/>
                    <MenuItem x:Name="MenuBarNesting" Header="{x:Static p:Resources.ContextMiseEnBarre}" Click="Open_MiseEnBarre"/>
                    <MenuItem x:Name="MenuExportDSTV1" Header="{x:Static  p:Resources.ContextExport}" Click="Export_DSTV"/>
                    <MenuItem x:Name="AjouterAffaire" Header="{x:Static  p:Resources.AjouterAffaire}" Click="AjoutAffaire"/>
                    <MenuItem x:Name="ModifyAffaire" Header="{x:Static  p:Resources.Edit}" Click="ModifierAffaire"/>
                </ContextMenu>
            </ListView.ContextMenu>
            <ListView.View>

                <GridView AllowsColumnReorder="true" x:Name="GridView1">
                    <GridViewColumn DisplayMemberBinding="{Binding Path=ID}" Header="ID" />
                    <GridViewColumn DisplayMemberBinding= "{Binding Nom}" Header="{x:Static p:Resources.Nom}"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Code}" Header="{x:Static p:Resources.Code}"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Comm}" Header="{x:Static p:Resources.Commentaire}"/>

                </GridView>
            </ListView.View>
        </ListView>

Then code behind is so :

public ObservableCollection<Affaire> listeAff { get; set; }
        public MainWindow()
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(Properties.Settings.Default.Langue);
            InitializeComponent();
            Initialisation();
            listeAff = new ObservableCollection<Affaire>();
            DBConnect DataBase = new DBConnect();
            string requete = "SELECT * FROM affaire ORDER BY ID";
            List<Affaire> liste = DataBase.Select_affaire(requete);
            foreach(Affaire aff in liste)
            {
                listeAff.Add(aff);
            }
            DataContext = this;

On that part my ListView is shown correctly, but when I want to delete(for example) some objects in list, listview is not updated :

foreach (Affaire aff in ListView1.SelectedItems)
                    {
                        listeAff.Remove(aff);
                    }

What did I miss?

you have to do as below for getting updated values

public class ChangeViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged=delegate {};

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

    ObservableCollection<Affaire> affaireListObservable;
    public ObservableCollection<Affaire> AffaireListObservable {
      get { return affaireListObservable; }
      set {
        affaireListObservable= value;
        if(PropertyChanged!=null) {
          NotifyPropertyChanged();
        }
      }
    }    
    public ChangeViewModel()
    {
        AffaireListObservable  = new ObservableCollection<Affaire>();
    }
    public void RemoveFromList()
    {
         foreach (Affaire aff in ListView1.SelectedItems)
         {
              AffaireListObservable.Remove(aff);
         }
    }
}

Double check you binding , As you are assigning DataContext to widnow, than you can go up in tree till window and use it , and bind you listview to DataContext of window

<ListView ItemsSource="{Binding Path=DataContext.listeAff ,
        RelativeSource={RelativeSource 
   Mode=FindAncestor,AncestorType=Window}}" 
  />

Once you delete or remove something from the collection, the UI has to be notified about it so that it updates itself accordingly. What you have to do is use INotifyPropertyChanged event in your class. You can learn all you want about it here

Happy learning and happy coding !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM