How can I keep track of state DRY-ly?

jporcenaluk

I am trying to keep track of state in a WPF application using MVVM. I just want there to be one state active at a time, but the way I'm currently doing it seems unreliable and not very DRY.

Here's an abstracted version of my code:

public class ApplicationStates
{
    public void SetStateOne()
    {
        IsThisStateOne = true;
        IsThisStateTwo = false;
        IsThisStateThree = false;
    }

    public void SetStateTwo()
    {
        IsThisStateOne = false;
        IsThisStateTwo = true;
        IsThisStateThree = false;
    }

    public void SetStateThree()
    {
        IsThisStateOne = false;
        IsThisStateTwo = false;
        IsThisStateThree = true;
    }

    public bool IsThisStateOne { get; set; }
    public bool IsThisStateTwo { get; set; }
    public bool IsThisStateThree { get; set; }
}

My plan is to bind my view's appropriate XAML objects to the boolean properties to maintain the view's state. I would like to continue having objects that I can bind to where they can do things depending on whether they are set to true or false.

Any ideas on making this more DRY, or is there a completely different, better way to architect this?

EDIT 1

I would like my XAML to look somewhat like this:

<Button Visibility="{Binding IsThisStateOne, Converter={StaticResource boolToVisibilityConverter}}"/>

<Button Visibility="{Binding IsThisStateTwo, Converter={StaticResource boolToVisibilityConverter}"/>

That way I can have what I need visible or not based on the state. And the state would change based on changes in the ViewModel.

Conceptually, since there are more than two states, I could make a converter that checks the name of a string converter parameter ("IsThisStateOne", "IsThisStateTwo") against the string of the enum, but that gets back into magic strings and duplication of non-type-safe code which I would like to avoid.

I understand databinding to the enum directly, I guess I just don't understand how to change the state of that enum (I can have a description string, but I don't think I can change it).

EDIT 2

Removed the enum. I don't really care if there is an enum involved or not, I just thought it would make things a little more strongly typed.

I guess I just want to know if there is a better, established design pattern for this type of requirement.

McGarnagle

I would keep the properties for simplicity of binding. But make them read-only, and keep a common variable (enum, string, int, whatever's easiest):

public void SetState(int stateNum)
{
    _state = stateNum;
}
private int _state = 1;

public bool IsThisStateOne { get { return _state == 1; } }
public bool IsThisStateTwo { get { return _state == 2; } }
public bool IsThisStateThree { get { return _state == 3; } }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I keep track of points in delphi

From Dev

How can I keep track of points in delphi

From Dev

How can i keep my code dry in ajax requests?

From Dev

How can I keep track of code folding in the code editor?

From Dev

How can I keep track of multiple downloads with NSURLSession?

From Dev

How can I keep track of an int and hold the 0's

From Dev

How can I keep track of the first launch of an application?

From Dev

How can I keep track of two lists in a function (fold) in OCaml?

From Dev

How can I keep track of array index in for loop?

From Dev

How can I keep track of a "like" system on android?

From Dev

How can I keep track of the first launch of an application?

From Dev

How can I keep track of data moving with dates?

From Dev

How can I keep track if runner is on pace in fitness tracking app?

From Dev

how can i recursively track empty folders and put an .keep file in it?

From Dev

How can I track state of different API calls in Redux?

From Dev

How aggressively should I subclass to keep DRY?

From Dev

How do I keep track of events in AngularJS?

From Dev

How do I keep track of events in AngularJS?

From Dev

How do I DRY-ly handle multiple upload types with Carrierwave?

From Dev

How can I keep track of where commits came from once they've been merged?

From Dev

How can I use a global PHP file to keep track of whether or not the user is logged in?

From Dev

How can I keep track of my stack trace height for debugging purposes?

From Dev

How can I keep track of each users disliked items and exclude them from the result in an efficient way

From Dev

How I can keep track of xml next children to print this html table with correct format using php

From Dev

How can I keep track of the coin number available coins in a vending machine?

From Dev

How can I keep track of each users disliked items and exclude them from the result in an efficient way

From Dev

How can I update a variable to keep track of the current value minus the previous value in Python?

From Dev

How can I sort a 2d array and keep track of the array indexes?

From Dev

How can I keep track of record number across multiple json objects in PHP

Related Related

  1. 1

    How can I keep track of points in delphi

  2. 2

    How can I keep track of points in delphi

  3. 3

    How can i keep my code dry in ajax requests?

  4. 4

    How can I keep track of code folding in the code editor?

  5. 5

    How can I keep track of multiple downloads with NSURLSession?

  6. 6

    How can I keep track of an int and hold the 0's

  7. 7

    How can I keep track of the first launch of an application?

  8. 8

    How can I keep track of two lists in a function (fold) in OCaml?

  9. 9

    How can I keep track of array index in for loop?

  10. 10

    How can I keep track of a "like" system on android?

  11. 11

    How can I keep track of the first launch of an application?

  12. 12

    How can I keep track of data moving with dates?

  13. 13

    How can I keep track if runner is on pace in fitness tracking app?

  14. 14

    how can i recursively track empty folders and put an .keep file in it?

  15. 15

    How can I track state of different API calls in Redux?

  16. 16

    How aggressively should I subclass to keep DRY?

  17. 17

    How do I keep track of events in AngularJS?

  18. 18

    How do I keep track of events in AngularJS?

  19. 19

    How do I DRY-ly handle multiple upload types with Carrierwave?

  20. 20

    How can I keep track of where commits came from once they've been merged?

  21. 21

    How can I use a global PHP file to keep track of whether or not the user is logged in?

  22. 22

    How can I keep track of my stack trace height for debugging purposes?

  23. 23

    How can I keep track of each users disliked items and exclude them from the result in an efficient way

  24. 24

    How I can keep track of xml next children to print this html table with correct format using php

  25. 25

    How can I keep track of the coin number available coins in a vending machine?

  26. 26

    How can I keep track of each users disliked items and exclude them from the result in an efficient way

  27. 27

    How can I update a variable to keep track of the current value minus the previous value in Python?

  28. 28

    How can I sort a 2d array and keep track of the array indexes?

  29. 29

    How can I keep track of record number across multiple json objects in PHP

HotTag

Archive