I am currently learning how to write a WPF application using the MVVM pattern. I am writing a small contact manager application, so my application displays a list attached to my view model and a set of fields related to ListBox.SelectedItem. One of these fields is contact photo.
I want to change the photo in the editing part using OpenFileDialog, so the Listbox element will be updated, as for all other fields.
At first I tried to update the source property of the Image control, but I lose the binding ... Then I wrote a Button_Click handler to update the Contact.Photo property (its type iste []), and it works. But instead of binding from the "update control" to the view model, binding from the VM to the control is as if the data were coming from the database.
(In the code LoadPhoto returns byte [])
private void Button_Click(object sender, RoutedEventArgs e) { OpenFileDialog OpenFileDialog = new OpenFileDialog(); if (OpenFileDialog.ShowDialog() == true) { (listbox.SelectedItem as ContactManager.ViewModel.Contact).Photo = LoadPhoto(OpenFileDialog.FileName); } }
I wonder if the MVVM pattern does not violate ... I'm not sure what can be done in the view ... Is it correct to change the Contact object? Does anyone have a better solution to this problem?
source share