What is the best way to implement a state machine UI?

In my program, I have three different user interface states (Normal, Success and Error), and in each of them the controls are visible / hidden, turned on / off, the colors change, the labels say different things ... etc. and in my codec I basically want to say ChangeWindowState (UI.Normal);

So my question is, what is the best way to implement management changes for each state?

Of course, I could manually change the controls in the code, but I'm wondering if maybe there is a better way using themes or wpf styles. Then, perhaps, I could just set the window to use the "Error" theme, which I previously defined. I don’t really understand them at the moment, so I might use the terminology incorrectly, but I would appreciate it if someone could point me in the right direction, how best to do something like this.

Thanks!

+5
source share
3 answers

, . , DataTemplates DataTriggers. , , : , , " " :

public partial class Window1 : Window
{
    public Window1()
    {
        this.InitializeComponent();

        // Insert code required on object creation below this point.
    }

    public ProgramStatus ProgramStatus
    {
        get { return (ProgramStatus)GetValue(ProgramStatusProperty); }
        set { SetValue(ProgramStatusProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ProgramStatus.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ProgramStatusProperty =
        DependencyProperty.Register("ProgramStatus", typeof(ProgramStatus), typeof(Window1), new UIPropertyMetadata(null));
}

public enum ProgramStatus
{
    Normal,
    Success,
    Error
}

( ), . :

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:l="clr-namespace:Test"
    x:Class="Test.Window1"
    x:Name="Window"
    Title="Window1"
    Width="640" Height="480">
    <Window.Style>
        <Style TargetType="{x:Type l:Window1}">
            <Style.Triggers>
                <Trigger Property="ProgramStatus">
                    <Trigger.Value>
                        <l:ProgramStatus>Error</l:ProgramStatus>
                    </Trigger.Value>
                    <Setter Property="Background" Value="Red" />
                </Trigger>
                <Trigger Property="ProgramStatus">
                    <Trigger.Value>
                        <l:ProgramStatus>Normal</l:ProgramStatus>
                    </Trigger.Value>
                    <Setter Property="Background" Value="Blue" />
                </Trigger>
                <Trigger Property="ProgramStatus">
                    <Trigger.Value>
                        <l:ProgramStatus>Success</l:ProgramStatus>
                    </Trigger.Value>
                    <Setter Property="Background" Value="Green" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Style>
    <Grid x:Name="LayoutRoot"/>
</Window>
+5

"UpdateUI()". / / /, /, . ( "ChangeWindowsState (..)" , "UpdateUI()" ).

I saw several attempts to deal with this in general terms ... but I did not like it (for example, WTL stuff). Typically, this is not poorly implemented ... but it is simply easy to quickly exceed what they can do. And, as a rule, the state logic is quite important, because if explicitly encoded with simple style logic, if / then / else leads to less confusion (maintenance, debugging, etc.).

+1
source

All Articles