switchable mandatory fields in wpf

switchable mandatory fields in wpf

Old forum URL: forums.lhotka.net/forums/t/10727.aspx

dan_morey posted on Friday, September 30, 2011

On my WPF interface I have, amongst other things, a checkbox, and two textboxes. When the checkbox isn't checked then I would like the two checkboxes to be blank and disabled. Then if the user checks the checkbox, the two checkboxes will become editable.

 My question is, where do I put the logic for this?

I'm using VS2010, with CSLA 4, and WPF using MVVM.

I know I can link the enabled property of the textboxes to something on the viewmodel, but I'm not sure whether I should just hard code it in the viewmodel or if it's possible to set up a rule on the business object that does it, and that I can then use to set the enabled properties.

RockfordLhotka replied on Friday, September 30, 2011

This is exactly what custom authorization rules are used to implement.

If you implement this through custom authorization rules, the CanWrite property of your PropertyStatus or PropertyInfo controls will expose the results as bindable values, and then you just bind the UI controls' IsEnabled properties to CanWrite. The enable/disable becomes automatic.

Custom authorization rules are discussed in the Using CSLA 4: Creating Business Objects ebook.

JonnyBee replied on Friday, September 30, 2011


A couple more things to remeber:

  1. You must remember to override CacheResult on the Authz rules.
  2. Use either CanWrite or StopIfNotCanWrite rules to control wether fields are required or not (based on CanWriteProperty)
    These rules exist in RuleTutorial sample (only in repository), at http://cslacontrib.codeplex.com  and http://cslagenfork.codeplex.com
    Look at the RuleTutorial on how to use them.
  3. You may also consider to add Authz rules to CanRead if you want to hide the real values and only return the default values for the value types. 

    public overide bool CacheResult
      get { return false; }

You could also look at BusinessRuleDemo project in Samples\Net\cs\BusinessRuleDemo on how State is required if country is US. (Albeit WinForms the concept in the BO is just same for WPF).

Copyright (c) Marimer LLC