Search code examples
c#wpfmvvm-light

MVVMLight C# how to change a button content


I'm new to WPF with MVVMLight and struggling to grasp how things work. I have a button in xaml:

<Button x:Name="button" Content="Button" 
    HorizontalAlignment="Left" 
    Margin="29,374,0,0" 
    VerticalAlignment="Top" Width="75" 
    Command="{Binding BeginCollectionCommand}"/>

And have the View Model respond to the button press. BeginCollectionCommand = new RelayCommand(BeginCollectionCommandExecute, () => true);

I failed to find the answer to my question on

  • How to set the button to disable
  • How to set the "content=" to "working..."
  • How to re-enable the button when the project is complete
  • How to set the "content=" to "Done"
  • I also want to wait 5 seconds to set the content to "Start" again. I believe i can do this with a thread.sleep(5000) but the other parts I'm not clear on.

The View Model code has the button binding "BeginCollectionCommand" defined as

public RelayCommand BeginCollectionCommand { get; set; }
    public MainWindowViewModel()
  {
    BeginCollectionCommand = new RelayCommand(BeginCollectionCommandExecute, () => true);
    //at this point i believe is where i set the button content to "working..."
    //and disable.
  }

  public void BeginCollectionCommandExecute()
  {
    /// do my working class code

    //I think at this point I want to set the code to change button content to
    //enable, conent to "done" then wait and set to "start"
  }

Can someone help?


Solution

  • You questions could be sum up to three kinds of question.

    1. How to enable or disable a button.
    2. How to change content of a button.
    3. How to change content after a period of time.

    For first and second question, bind your button IsEnable to a property in viewModel and bind content to a string

    In xaml.

    <Button x:Name="button" 
        Content="{Binding ButtonString}"
        HorizontalAlignment="Left" 
        Margin="29,374,0,0" 
        VerticalAlignment="Top" Width="75"
        IsEnabled="{Binding ButtonEnabled}"
        Command="{Binding BeginCollectionCommand}"/>
    

    In view model

        // Set true or button cannot be pressed.
        bool m_Enabled = true;
        public bool ButtonEnabled
        {
            get{  return m_Enalbed; }
            set{ m_Enabled = value; 
             // RaisePropertyChanged MUST fire the same case-sensitive name of property
                 RaisePropertyChanged( "ButtonEnabled" );
               }
            }
        }
    
        public bool ButtonString
        {
         get;set;
        }
        bool m_String = false;
        public bool ButtonString
        {
            get{  return m_String; }
            set{ m_String = value; 
                 // RaisePropertyChanged MUST fire the same case-sensitive name of property
                 RaisePropertyChanged( "ButtonString" );
               }
            }
        }
    
    
    public void BeginCollectionCommandExecute()
    {
        //I simplify the way of variable passing, 
        //You need to take care of how to set property from command to viewmodel. 
        //A method delegate would be okay.
        ButtonEnabled = false;
        ButtonString = "Working";
        // working here
        ButtonEnabled = true;
        ButtonString = "Done";
    }
    

    For the third question, you may use a timer or a ThreadSleep is okay.