views:

745

answers:

2

Hello.

I'm trying to bind a HashSet to a ListView item. I've documented my code here:

public class Person {
    public string Name { get; set; }
    public AddressList = new AddressList ();
}
public class AddressList : HashSet<Addresses>
{
    //
}
public class Addresses {
    public string Streetname { get; set; }
    public string City { get; set; }
}
public class PersonViewModel : INotifyPropertyChanged {
    private Person _person;

    public PersonViewModel(Person person)
    {
        _person= person; 
    }

    public string Name
    {
        get { return _person.Name; }
        set
        {
            _person.Name = value;
            OnPropertyChanged("Name");
        }
    }
    private void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

 // This is how I add the DataContext: mainGrid.DataContext = _person //this is a PersonViewModel();
 // This is how I bind the DataObjects to the GUI Elements: <TextBox Name="TxtBoxName" Text="{Binding Path=.Name, Mode=TwoWay}"/>
 // How can I add in the same way a HashSet to a ListView Element in the WPF Gui? I tried something like {Binding Path=.Name, Mode=TwoWay}

Can anyone help me with tipps how to accomplish that? Thanks a lot!

Cheers

+1  A: 

To bind a collection to a ListView (or any ItemsControl, for that matter), you need to set its ItemsSource property. This should be bound to an instance of your AddressList class, assuming that collection is what you want displayed in your list.

Once you've done that, you'll need to set up the bindings for each column in the ListView, similar to how your comment at the bottom of the sample code describes it.

Andy
+1  A: 

This example binds to an XML data source, but you should be able to adapt it to your needs.

See also the MSDN documentation for ListView here.

micahtan