0

I have a BindingList<Pair<string,string>> (where Pair is just your bog standard generic to contain two related objects). I want to be able to bind this to a ListView such that the .First values is in column 1 and the .Second value is in column 2. Further to this, how can I make sure the list view constantly represents the contents of the list, such that if I change one of the strings the ListView automatically updates?

Here is my Pair<TI,TJ> class for reference:

public class Pair<TI, TJ>
{
    public TI First;
    public TJ Second;

    public Pair(TI first, TJ second)
    {
        First = first;
        Second = second;
    }
}

Just to clarify, a BindingList is just the collection I'm currently trying to use, I can use any collection capable of supporting this functionality and holding Pair<string,string>.

ScottishTapWater
  • 3,656
  • 4
  • 38
  • 81
  • `ListView` doesn't have data-binding support. Use `DataGridView` instead. Also to have 2-way data-binding, you need to implement `INotifyPropertyChanged` for your `Pair` class. – Reza Aghaei Nov 25 '16 at 12:51

2 Answers2

0

I don't have the IDE at hand, but it depends mostly on if you use WinForms or XAML/WPF. Since it is probably the last (more modern), some tips:

  • Probably you need an ObservableCollection, if something changes in the collection the bindings will work.
  • For binding to your data, you need to bind them in WPF, and set a data context.

In general it looks like this:

<ListView ItemsSource="{Binding ListOfYourData}"
        SelectedItem="{Binding Path=SelectedItem}"
 ...

<ListView.View>
  <GridView>
    <GridViewColumn Width="140" Header="First"
       DisplayMemberBinding="{Binding First}"  
    <GridViewColumn Width="140" Header="Second"
       DisplayMemberBinding="{Binding Second}"  
Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
0

There is two important interfaces for databinding with Winforms.

IBindingList - This one concern "Collection" and will update the control when a new element is added or when an element is removed. The BindingList already implement it so you don't have to take care about. You could test with a simple List if you want to test the behaviour without this interface.

INotifyPropertyChanged - This one concern "Object" and will update the control when an object have a change one of his properties (the value of the object has changed). Your class "Pair" should implement this interface. It is trivial. You juste have to add a Sub PropertyChanged() which raise a event and add a call of this Sub in the setter of your properties.

You could have a look at the MSDN if you want a sample.

https://msdn.microsoft.com/fr-fr/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

Resume :

No IBindingList => Control doesn't update when you add or remove object in a collection.

No INotifyPropertyChanged => Control doesn't update when a property of an object have changed.

Edit - Of course the control have to support Binding which is not the case of the legacy ListView...

Marco Guignard
  • 613
  • 3
  • 9