0

I've got a ListBox on a window with some other components. When I change the Visibility of these other components, the ListBox fires its SelectionChanged event with the new selectedIndex = 0. That's very undesirable. (It doesn't happen if you insert breakpoints, or, presumably, Sleeps).

I want a reliable event that only fires when the user actually changes the ListBox selection, not when WPF merely changes the window layout. Does such a thing exist, or for something more robust should I just build my own control from scratch using buttons?

thund
  • 1,842
  • 2
  • 21
  • 31
  • What do you want `SelectionChanged` for? In all these years doing WPF, I never used that. Not even once. What are you trying to do? BTW, post your current code and XAML. – Federico Berasategui Feb 07 '14 at 20:53
  • I want to know when a user uses the mouse or keyboard to change the listbox selection. So how should you do this? Current code is too big to post. I haven't got a small example right now. – thund Feb 07 '14 at 20:58
  • @thund - That's not true. `SelectionChanged` never gets fire on visibility change of other controls. You gotta post relevant small sample code here to replicate an issue. – Rohit Vats Feb 08 '14 at 11:26
  • *Never???* That's an impressive statement ;) Obviously I think I have a counterexample. It's currently big and complex, so I'll have to whittle it down to something postable if it's of interest. Meanwhile I've built my own control from scratch, which seems to suffer no such oddities. – thund Feb 09 '14 at 00:54

1 Answers1

0

for something more robust

If you want a robust application, you need a robust design.

If you're working with WPF, You need to leave behind the traditional event-based approach and understand and embrace The WPF Mentality.

I want to know when a user uses the mouse or keyboard to change the listbox selection

Instead of handling events, putting a bunch of code behind, and hoping that the complexities of the Visual Tree will allow that to work, simply use proper DataBinding:

<ListBox ItemsSource="{Binding SomeCollection}"
         SelectedItem="{Binding SelectedItem}"/>

to a proper ViewModel:

public class MyViewModel
{
    public ObservableCollection<MyClass> SomeCollection {get;set;}

    public MyClass SelectedItem {get;set;} //Don't forget INotifyPropertyChanged
}
  • See how I'm not handling any events or putting any code behind. The Visual tree can do whatever it wants and raise as many events, and my code will still work.
  • Also see how this approach is much cleaner because it allows a true separation between UI and data.
  • I could go on forever about the advantages of proper MVVM, but I'm too lazy right now. Let me know if you need further help.
Community
  • 1
  • 1
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • Thanks. Yes, I'm familiar with the WPF mentality and have several thousand lines of data-bound code. However, I've also spent countless hours tracking down obscure bugs arising from it, and sometimes I want something cleaner and simpler that I can control completely myself, rather than hoping I don't run into any WPF "undocumented features". This is such a time. I just want the ListBox to tell me when the user's changed his selection. That seems like a fairly reasonable thing for it to provide. – thund Feb 07 '14 at 22:19
  • `obscure bugs arising from it` - You mean `obscure bugs arising from not implementing it properly` - your statement that you `want something simpler that you can control` evidences your lack of proper understanding of how WPF works and is supposed to be used. – Federico Berasategui Feb 07 '14 at 22:22
  • No I don't mean that. I take full responsibility for my own bugs, I find them, and I fix them. But WPF itself is pretty buggy too. And if I want something that also runs on Windows Phone, RT, Silverlight ;), or .NET3.5, then the inconsistencies and bugs just multiply. So rather than spending hours on forums trying to nail down MS platform-dependent bugs and kill them off one by one with #ifs, I'd often rather just build something clean from scratch myself. But whatever my odd reasons, MS does provide us with ListBox events. So I think it's perfectly valid that I ask for a *useful* one. – thund Feb 07 '14 at 22:51
  • @thund OK, if you have a concrete example of such problems, please show it to me. I could help you thru it and learn something as we go. – Federico Berasategui Feb 07 '14 at 22:58
  • Thanks, I'll let you know the next time one comes up. But right now I'm working on a different approach.. – thund Feb 07 '14 at 23:10