2

Below is an mcve (to reproduce the problem it has to be datatemplated unloadable view).

xaml:

<Window Content="{Binding}" ... >
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:ViewModel}">
            <ListView ItemsSource="{Binding Items}"
                      SelectionChanged="ListView_SelectionChanged"
                      Unloaded="ListView_Unloaded"
                      MouseRightButtonDown="ListView_MouseRightButtonDown">
            </ListView>
        </DataTemplate>
    </Window.Resources>
</Window>

cs:

public class ViewModel
{
    public ObservableCollection<string> Items { get; set; } = new ObservableCollection<string>(new[] { "1", "2", "3" });
}

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }

    void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e) => Title += "S";

    void ListView_Unloaded(object sender, RoutedEventArgs e) => Title += "U";

    void ListView_MouseRightButtonDown(object sender, MouseButtonEventArgs e) => DataContext = null;
}

Start program, select item in the list ("S" will be added to title), right click inside list -> "SU" will be added to title.

Question: why SelectionChanged is called when view (ListView) is unloaded??

Right-clicking without selecting anything will add only "U" to the tittle.

Simply closing software (disregarding selection) will not cause SelectionChanged (tested by setting breakpoint).

I will have some logic in SelectionChanged event and I don't want it to run when view is unloaded, it's unexpected behavior what SelectionChanged event is called in this case at all.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • What is the `SelectedItem` at that point? Is it `null`? – Mike Eason Jun 01 '16 at 12:47
  • Because whatever you had selected is gone now. And the selectedItem will be null. – Nawed Nabi Zada Jun 01 '16 at 12:50
  • You are both right, it's `null`, but why then I don't get same event when I simply close window? Who is setting selection to `null` and cause event? How can I prevent/control this? I know keyword is *datatemplating*, but knowing only that is not really helpful. – Sinatr Jun 01 '16 at 13:13
  • Because the selection changes when the control is unloaded? Why doesn't really matter, what you need is a workaround. That means either don't do this work within the event (btw, doesn't sound very MVVM to me), or do something else, like extend the control and suppress the event on unload. –  Jun 01 '16 at 14:01
  • @Will, see my [previous question](http://stackoverflow.com/q/37541929/1997232) for reasons behind using code in the view. Following your suggestion, how do I know what view is about to be unloaded? In real project not View, nor ViewModel knows what view is unloaded and it would be really problematic to inform either about action which switch the view. Here as you can see `Unloaded` is fired after `SelectionChanged`. I need some kind of `Unloading` event (similar to winforms `Closing/Closed`). Suggestions? What kind of *extending* control do you mean? – Sinatr Jun 01 '16 at 14:14
  • Offhand I don't know. My first instinct (if I couldn't figure out how to refactor my code so that the situation was avoided altogether) would be to extend the control and override the OnUnloading and OnSelectionChanging events (or similar) and make sure that SelectionChanged doesn't fire on unload. Or maybe I can look at the control within the SelectionChanged event handler and see if it is unloading. I don't have time to research this, I just wanted to drop a couple suggestions in a comment. Check out the docs and see if any of them are possible. –  Jun 01 '16 at 14:18
  • @Will, thanks for idea, though I don't think it works this way. It has to be something to do with datatemplate, rather than with the control (otherwise it would behave similarly when form is closed and without any datatemplating). That was my very recent thought (so it doesn't really fit to `Unloading` event comment given earlier). – Sinatr Jun 01 '16 at 14:24

0 Answers0