Change Button Content and Text Based On Previous Click

If you are using MVVM, bind the content to a value and bind the command to function.

<Button Content="{Binding ButtonText}" Command="{Binding ButtonClickCommand}"/>

Of course, you then have String ButtonText and ButtonClickCommand as properties in your ViewModel.

private string _ButtonText;
public string ButtonText
{
    get { return _ButtonText ?? (_ButtonText = "Add"); }
    set
    { 
        _ButtonText = value;
        NotifyPropertyChanged("ButtonText"); 
    }
}

private ICommand _ButtonClickCommand;
public ICommand ButtonClickCommand
{
    get { return _ButtonClickCommand ?? (_ButtonClickCommand = _AddCommand); }
    set
    {
        _ButtonClickCommand = value;
        NotifyPropertyChanged("ButtonClickCommand");
    }
} 

private ICommand _AddCommand = new RelayCommand(f => Add());
private ICommand _SaveCommand = new RelayCommand(f => Save());

private void Add()
{
    // Add your stuff here

    // Now switch the button   
    ButtonText = "Save";
    ButtonClickCommand = SaveCommand;
}

private void Save()
{
    // Save your stuff here

    // Now switch the button   
    ButtonText = "Add";
    ButtonClickCommand = AddCommand;
}

Then you can have the ButtonClickCommand change the properties and binding takes care of everything.


An alternate design could be if one created 2 buttons such as AddButton and SaveButton, then one could show or hide them respectively (using Visibility property).

Why?

Because it is a Matter of Separation of Concerns. For example, in the click handler you wouldn't need to check the mode you're in, because you'll have separate handlers. You will also want the buttons to have different icons, different Tooltips, etc.


Store the value of last click in the tag property of that button and check for its value on click.

Tag Description

Gets or sets an arbitrary object value that can be used to store custom information about this element.

MSDN Link

OR

void MyButton_OnClick(object sender, RoutedEventArgs e)
{
    if(mybutton.Content.ToString() == "Add")
    {
        \\ Lines for add
        mybutton.Content = "Save";
    }
    else
    {
        \\ Lines for Save
        mybutton.Content = "Add";    
    }
}

I agree with Surfens answer that the question here is not a perfect example for a ToggleButton because "Save" and "Add" a really different operations which should each have the own "ICommand" set on the respective button.

But here is some style that will change the content depending on the IsChecked value of the ToggleButton.

The content will be "ValueForUnToggledState" if the button is not checked and change to "ValueForToggledState" when checked.

<ToggleButton>
    <ToggleButton.Style>
        <Style TargetType="{x:Type ToggleButton}">
            <Setter Property="Content" Value="ValueForUnToggledState" />
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Content" Value="ValueForToggledState" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ToggleButton.Style>
</ToggleButton>

This is more WPF like than some of the other answers.

Tags:

Windows

C#

.Net

Wpf