1

I need to be able to "Freeze" a Listbox selection until I clear the User's selection of an item.
I can't seem to find any examples and need a little pointer in the right direction.
I'm in MVVM so I'll probably want to fire a RelayCommand and only enable it when the SelectedIndex is -1.

How do I do this?

Johan
  • 74,508
  • 24
  • 191
  • 319
Rachael
  • 1,965
  • 4
  • 29
  • 55
  • do you mean prevent the user from selecting something else or prevent the user from using the listbox at all? (graying it out) – aL3891 Oct 11 '13 at 18:59
  • prevent the user from selecting something else. probably throwing up a message box or something so the user can be instructed to press the refresh button to clear the selection and enable changing the selection once again. – Rachael Oct 11 '13 at 19:04

2 Answers2

2

You could try to use the Enable propety like:

<Listbox Enabled=False />

If you want, you can bingint this property with another in your code.

Property IsListBoxEnable As Boolean
<Listbox Enabled="{Binding IsListBoxEnable, Mode=TwoWay}" />

and in your code you just modify the value of IsLisBoxEnable.

Alexandre
  • 498
  • 2
  • 12
  • That's a good idea. But doing this in MVVM (without getting too messy) I need to also be able to re-enable it. I would assume I need to deal with a command in said case? – Rachael Oct 11 '13 at 19:03
  • I dont know if i understood what you say, but to re-enable if you have made the binding, you just have to set 'IsListBoxEnable = true' – Alexandre Oct 11 '13 at 19:05
  • Ah. I was having a slow moment. I see what I can do with this here. This is very simple and nice. Perfect! I believe this prevents me from having to write the converter in @aL3891 's answer above? – Rachael Oct 11 '13 at 19:18
  • it does :) it your choice whether to have an extra property in your VM or write the converter, both are valid options – aL3891 Oct 11 '13 at 19:29
2

One way to do this is to data bind IsEnabled to SelectedIndex and then have a converter that returns true only if SelectedIndex is -1

<Listbox IsEnabled={Binding RelativeSource={RelativeSource Self}, 
                    Path=SelectedIndex, Converter=YourConverter} />

For more info on writing YourConverter see this page

In you viewmodel you can have a property for SelectedIndex and then just set it to -1, and the listbox should be enabled again


If you don't want to disable the listbox completely, you can use datavalidation to show an error message under some conditions, here is a good place to get started

aL3891
  • 6,205
  • 3
  • 33
  • 37
  • This sounds great. So would I still be able to do something on the event GetFocus when the listbox is not enabled? I guess I can just do that for the container of the listbox in that case (when the user is trying to select but they have already made a selection) – Rachael Oct 11 '13 at 19:15
  • hm I wonder.. if you need that, you might be better off with data validation. I'm not entirely sure wpf data validation prevents you from selecting other values though, but you could do that via data binding instead. – aL3891 Oct 11 '13 at 19:18
  • I ended up going with the above, but this is a great solution. Thanks very much and it definitely gets me thinking about using converters more. – Rachael Oct 11 '13 at 22:35