Note that there are some explanatory texts on larger screens.

plurals
  1. POOpening a new dialog using WPF with MVVM
    primarykey
    data
    text
    <p>I am currently using MVVM (Light) to build an application with WPF. However, in a few cases I must open a new dialog (also WPF) when the user clicks a button. However, this is being a tough fight.</p> <p>Here is how I am doing it:</p> <pre><code> private void _ShowItemDialog(Item item) { var itemVM = new ItemViewModel(); itemVM.CurrentItem = item ?? new Item(); itemVM.Load(); var itemView = new View.ItemView() { DataContext = itemVM }; if (itemView.ShowDialog() == true) { if (item == null) { itemList.Add(itemVM.CurrentItem); } } itemVM.Cleanup(); } </code></pre> <p>And the itemView XAML there is no binding to the DataContext, otherwise two different instances of the ViewModel would be created.</p> <p>Inside the Window tag. To have the result at ShowDialog, I use the DialogCloser code:</p> <pre><code> public static class DialogCloser { public static readonly DependencyProperty DialogResultProperty = DependencyProperty.RegisterAttached( "DialogResult", typeof(bool?), typeof(DialogCloser), new PropertyMetadata(DialogResultChanged)); private static void DialogResultChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var window = d as Window; if (window != null) window.DialogResult = e.NewValue as bool?; } public static void SetDialogResult(Window target, bool? value) { target.SetValue(DialogResultProperty, value); } } </code></pre> <p>In the ItemView, this is declared inside Window tag as follows:</p> <pre><code>my:DialogCloser.DialogResult="{Binding DialogResult}" </code></pre> <p>And when the dialog is closed, the closing event sets DialogResult to true or false.</p> <p>This works perfectly for the first time the screen is opened, but it is not possible to open the dialog again after it is closed.</p> <p>I would like to know if you have any better ideas for opening the dialog, and why this code does not work.</p> <p>Thanks!</p> <p><strong>EDIT:</strong> I have already fixed the code. What I need to do is create a new ViewModel and attach it to the DataContext every time the dialog is opened. Moreover, I had to remove the DataContext binding from XAML. Please check the code changes above.</p> <p>With these changes I have found out that it is not possible to use the ViewModel from ViewModelLocator because it is a "singleton" and not a new instance at each new window. Therefore, the DialogResult held the last value and if I tried to change its value back to null (as it is when the ViewModel is initialized) an exception is thrown. Do you have any clues of why this happens? It would be very good for me to use the ViewModel from ViewModelLocator, since it would keep the same strategy throughout the system.</p> <p>Thank you!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload