views:

298

answers:

3

Hello, everyone. I need a ListBox which will contain several options. I need checkboxes exactly(style), not radio buttons. Is there any way i can allow only 1 checked checkbox at the moment? I'm using MVVM, so i can't just check or uncheck them manually, it's against the rules. And if i can't make such functionality - is there easy way to style radiobuttons to look like checkboxes?

A: 

Aside from a flawed requirement*, the only way to do this is to uncheck all checkboxes, then check the particular indexed checkbox.

Or alternately (cos it does the same thing, but sounds longer), iterate through all the indicated checkboxes and find whichever one is set to true that is not the one you want checked, then set it to false.

  • flawed requirement: A series of checkboxes indicates to any user that they are allowed to select zero or more items. A series of radiobuttons indicates that they are allowed to select only one. This is something that has been drilled into users since before Windows 3, and that all non-IT will not question. You'll break their mental model, which is worse than looking pretty. Please have management revise this requirement.

HTTH. YMMV.

drachenstern
Well, that's not my requirement. So it's indisputable, unfortunately. How can i uncheck all the chekboxes at once?
Walkor
@walkor ~ I don't guess I understand the question. What is it you don't know how to do? `foreach (CheckBox cbx in CheckBoxList) { cbx.Checked = false; }` or that you don't know how to select all the checkboxes that are related or ... I would think the advice to uncheck all the checkboxes is straightforward, but I've been doing it for a long time, so maybe the advice is not straightforward. You tell me what you don't understand. How are you generating your checkboxes in the first place? Update your question.
drachenstern
My question is clear enough, i don't have code behind in xaml.cs file. And i can't call this - foreach (CheckBox cbx in CheckBoxList) in MVVM.
Walkor
@Walkor ~ Can you do it on the client? Use jQuery or similar library and grab all the checkboxes in that particular div or tr or whatever. Same logic applies. || At some point you have a model of all the checkboxes and the response from the user. That's when you do your check/uncheck. The binding will carry back to view regen.
drachenstern
A: 

If you are using MVVM and what to stick to the "rules" then your ViewModel should have a property to which the checkboxes bind. Its the up to code in the ViewModel to ensure that state of this property is correct.

So code in the ViewModel where one property gets set to true may need hunt through a collection to find similar items whose matching property needs to be forced to false. The View then simply reflects the current state of the ViewModel.

AnthonyWJones
Well, yes, i thought about this idea, but also, i hoped that there is a better way for this. Just to make sure - you suggested to iterate through item collection, mark items with checked/unchecked states and make binding reflect those changes, right?
Walkor
A: 

Well, in the end, i used this solution

Walkor