D
D
Dmitry Kalinnikov2015-04-25 07:53:01
User interface
Dmitry Kalinnikov, 2015-04-25 07:53:01

How to edit a list item in WPF?

Greetings.
There is a WPF form with a list on it

<ListView x:Name="FilmListView" 
                  ItemsSource="{Binding Path=FilmList}" 
                  ItemTemplate="{StaticResource FilmView}"
                  SelectedItem="{Binding Path=SelectedFilm, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"
                  IsSynchronizedWithCurrentItem="True"/>

the list is bound to an ObservableCollection of QFilm objects, with some sort of base type fields.
Started making fields for editing properties.
<WrapPanel>
          <TextBlock Text="Name: " />
          <TextBlock Text="{Binding Path=SelectedFilm.Name, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
</WrapPanel>

<WrapPanel>
          <TextBlock Text="Name: " />
          <TextBox Text="{Binding Path=EditFilm.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</WrapPanel>

Those. when a line is selected in the list, the corresponding object is placed in the SelectedFilm. EditFilm must somehow get the data from this object and copy it back on the "save" command.
Does not work. GUI events cannot be used because MVVM.
I tried to subscribe EditFilm.Copy(SelectedFilm) through the selected method to SelectionFilm.PropertyChanged - it did not help, because when the selection is changed, this event does not fire (QFilm implements INPC and the Copy method, which directly assigns properties bypassing the Notify interface).
What to do? Usually I try to figure it out myself, but here - a stupor and it is not clear where to dig. An architectural problem, so to speak.
And, yes, I would like to do it without using third-party libraries and so on. I'm sure there is an idiotically simple solution, but I just don't see it point-blank.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir S, 2015-04-25
@Growman

There are two options, the first one is editing SelectedFilm directly (I think it's the best option - you change it, press the Save button to apply the changes),
and the second is to link SelectedFilm and EditFilm.
If you have SelectedFilm and EditFilm defined in the view model, then you need to add the transfer of the SelectedFilm link to EditFilm and back after the change. For example, the selected element has changed, the view model copies a link to it in EditFilm. Click the save button, the data from the EditFilm is copied to the selected element, and the EditFilm is set to zero.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question