c# - Architecture: How to implement status which affects several UI objects? -


i can come solutions following problem find quite unsatisfying:

i want several ui elements change behaviour (mostly dis/enable or change visibility) depending on status (integer between 0-8) want able modify several places in application.

is there more elegant way implementing huge switch/case block in setter of status property (implementing strategy pattern doesn't seem better solution since switching 'some' flags)? maybe can magic databinding here? use dedicated flag in viewmodel ui-object-properties need change (which in turn modified in setter of status-property...)

i guess sort of conditional databinding elegant thing can think of, input if there other viable ways implement this.

you use converter designed convert status (int) wether or not control enabled (bool).

[valueconversion(typeof(int), typeof(bool))] public class statustoenabledconverter: ivalueconverter {     public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         int status = (int)value;          switch (status)         {             case 1: return true;             case 2: return false;             default: return false;         }     }      public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         throw new notimplementedexception();     } } 

then, can bind isenabled of controls want influence status code, converting converter.

<window x:class="wpfapplication2.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:local="clr-namespace:wpfapplication2"     title="mainwindow" height="350" width="525"> <window.resources>     <local:statustoenabledconverter x:key="statusconvert"/> </window.resources> <grid>     <button isenabled="{binding status, converter={staticresource statusconvert}}" /> </grid> </window> 

for visibility, exact same thing, using statustovisibilityconverter , return visibility.visible, or visibility.collapsed.

...or if want technical , reuse previous converter, design booltovisibilityconverter , chain converter above new one.


Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -