将Listview绑定到ObservableCollection

时间:2017-12-07 05:57:59

标签: c# listview binding observablecollection

我正在尝试将listview绑定到ObservableCollection。

我在堆栈上看了几个主题,但找不到我做错了什么:

我的XAML看起来像那样(对于GridViewColumn,我尝试了两个路径=,没有,因为我看到了两个):

<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>

然后代码就是这样:

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;

在那部分我的ListView显示正确,但是当我想删除(例如)列表中的某些对象时,listview不会更新:

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

我错过了什么?

2 个答案:

答案 0 :(得分:2)

您必须按照以下方式获取更新值

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

仔细检查绑定,当您将DataContext分配给widnow时,您可以在树中直到窗口并使用它,并将listview绑定到窗口的DataContext

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

答案 1 :(得分:2)

从集合中删除或删除某些内容后,必须通知UI,以便相应地更新自身。您要做的是在课堂上使用 INotifyPropertyChanged 事件。您可以了解所有您想要的内容here

快乐学习和快乐编码!!