WPF - using data binding to bind list of items and detail text boxes

I am trying to set up data binding as described in the title.

The problem I am having is with a shared list.

Any examples.

I cannot use the BindingListCollectionView on the general list, so you need to use a CollectionView.

The problem I am puzzled about is adding a new item when I click the Add button I add a new item to the shared list and refresh the view. But if the user does not go through the list, the item is now empty.

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

Malcolm

+1


source to share


1 answer


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

Item List with Detail View

Given these ViewModel classes (imagine each one 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; }
}

      



Put an instance Data

in DataContext

, the minimal view might look like this:

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

      

Adding new elements

To create a new Item

one without immediately adding it to the list, you may want to detach the newly created object in its scope. Visually, you can either add it to the new popup or include it in the list, but in reality it will only be added to the list the next time you try to add or confirm the parent dialog. At this point, you can also check if it is enough Item

to add it to the list.

+3


source







All Articles