1

Please excuse my ignorance for I am new to C#.

I am currently working on an MVVM project in which a viewmodel has multiple instantiated public variables that are data-bound to elements in a view (WPF). When these variables are changed they automatically update in my view. Take for instance the code segment below from my view model...

    private string _displaybind;

    public string DisplayBind
    {
        get { return _displaybind; }
        set
        {
            SetProperty(ref _displaybind, value);
            if (_displaybind.Length > 5000)
            {
                DisplayBind = _displaybind.Substring(_displaybind.IndexOf('\n') + 1);
            }
        }
    }

By using the command DisplayBind = "Hello"; within my viewmodel I can push out text to a textbox I have located in my XAML view. Unfortunately, I have reached a point where I can not simply edit the value of DisplayBind.

I need to start a state machine within my viewmodel which will access several states (classes) in separate C# files. However, I have no idea how to receive, and more importantly edit the values within my viewmodel from these separate classes.

I start my state machine in my viewmodel using this...

                    IPMProgram ipmprogram = new IPMProgram();
                ipmprogram.StartTheIPMProgram();

This is my IPMProgram class

    public class IPMProgram
{
    public IPMProgramState currentState = null;

    public IPMProgram()
    {
        currentState = new BootBannerState(this);
    }

    public void StartTheIPMProgram()
    {
        while (true)
        {
            currentState.GetNextState();
        }
    }
}

This is my IPMProgramState class

    public abstract class IPMProgramState
{
    private IPMProgram ipmprogram;

    public IPMProgram Ipmprogram
    {
        get { return ipmprogram; }
        set { ipmprogram = value; }
    }

    public abstract void GetNextState();
}

And this is my BootBannerState class (The state I want to edit DisplayBind from)

    class BootBannerState : IPMProgramState
{
    public BootBannerState(IPMProgramState state)
        :this(state.Ipmprogram)
    {

    }

    public BootBannerState(IPMProgram ipmprograminstance)
    {
        this.Ipmprogram = ipmprograminstance;
    }

    public override void GetNextState()
    {
        //DisplayBind = "Hello"!
        return;
    }
}

Someone suggested that I should look into Dependency Injection, but I don't quite understand how it would work for me. What should I do?

Thank you for all of your help,

Tesnich

Tesnich
  • 37
  • 4
  • Something to read that may help you: http://prismlibrary.readthedocs.io/en/latest/WPF/03-Managing-Dependencies/ – R. Richards Mar 29 '17 at 23:33

0 Answers0