c# - Silverlight Checkbox binding - change checked state in View Model property -


i'm pulling what's remaining of hair out trying work in silverlight works out of box in wpf.

i have checkboxes on form represent items - checked property bound bool in viewmodel (one viewmodel per item). when checkbox checked adds item list in viewmodel - before happens want perform validation (in case count how many items in list in other viewmodel, , if reached limit show user message) , if validation fails don't add list , uncheck box. when runs, after validation check done in bool property setter can see value set false, not reflected in checkbox in ui in silverlight checkbox remains checked. in wpf issue not occur.

the following code demonstrates issue - brevity instead of performing validation i'm forcing box unchecked when checked.

xaml

<usercontrol x:class="checkboxtest.sl.mainpage"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     mc:ignorable="d"     d:designheight="300" d:designwidth="400">     <grid x:name="layoutroot" background="white">         <checkbox ischecked="{binding isselected, mode=twoway}" margin="20"/>     </grid> </usercontrol> 

code behind

namespace checkboxtest.sl {     public partial class mainpage : usercontrol     {         public mainpage()         {             initializecomponent();              this.datacontext = new mainpageviewmodel();         }     }      public class mainpageviewmodel : inotifypropertychanged     {         private bool _isselected;          public bool isselected         {             { return _isselected; }             set             {                 _isselected = value;                  if (_isselected) //if checked uncheck                     _isselected = false; //this not reflected in ui in silverlight                  onpropertychanged("isselected");             }         }          public event propertychangedeventhandler propertychanged;          protected void onpropertychanged(string propertyname)         {             propertychangedeventhandler changedeventhandler = this.propertychanged;             if (changedeventhandler == null)                 return;             changedeventhandler((object)this, new propertychangedeventargs(propertyname));         }     } } 

i know people used round issue when in wpf pre .net 4.0 either setting property binding async (not allowed in silverlight) or implement dummy ivalueconverter (tried , had no effect in silverlight).

can suggest way above work in silverlight please?

one way bind isenabled property of checkbox boolean property in viewmodel can perform validation.

public bool caneditcheckbox {         {         // perform validation here         return list.length < 100;     } } 

edit

i tested code, , seems work intended.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -