views:

2076

answers:

2

Hi,

I am trying set up databinding as described in the title.

The problem I having is binding to a generic list.

Any examples out there.

I can't use BindingListCollectionView on a generic list so have to use CollectionView.

The issue I am puzzled about is to add a new item on click of Add button I add a new item to the generic list and refresh the View. But if user does not follow through the list now has empty item.

I know this is basic but how is that handled normally?

Malcolm

+2  A: 

I see two questions here and I'll try to answer them step by step.

Binding list of items with detail view

Given these ViewModel classes (imagine everyone implements INotifyPRopertyChanged):

public class DataView {
    public Item SelectedItem {get; set; }
    public List<Item> Items { get; private set; }
}

public class Item {
    public string Title { get; set; }
}

Putting a Data instance into the DataContext, a minimal View might look like this:

<StackPanel>
    <ListView Items="{Binding Items}" SelectedItem="{Binding SelectedItem}" />
    <TextBox Text="{Binding SelectedItem.SelectedItem.Title}" />
</StackPanel>

Adding new items

To be able to create a new Item without immediately adding it to the list, you might want to split off the newly created object into its own area. Visually you can either have it in a new pop up or integrated into the list, but actually it'll be only added to the list on the next try to add or acknowledge the parent dialog. At this point you can also verify whether the Item is valid enough to add it to the list.

David Schmitt
A: 

My question is really is there built in support for adding a new item to a collection in the manner I described to a generic list with databinding.

So if there is not I will have to work on out that suits my situation.

But I take your ideas in David thanks.

Malcolm