Change Button Content and Text Based On Previous Click

57,484

Solution 1

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";    
    }
}

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

You can also use a ToggleButton with EventTriggers to execute the different methods for Checked and Unchecked states.

<ToggleButton x:Name="ToggleButton" Content="Add"
              Style="{StaticResource ToggleStyle}"
              IsThreeState="False">
     <i:Interaction.Triggers>
      <i:EventTrigger EventName="Checked">
      <ei:CallMethodAction MethodName="Save" TargetObject="{Binding}" />
     </i:EventTrigger>
     <i:EventTrigger EventName="Unchecked">
       <ei:CallMethodAction MethodName="Add" TargetObject="{Binding}" />
     </i:EventTrigger>
 </i:Interaction.Triggers>
 </ToggleButton>

You can also use a style to modify the ToggleButton template and change the text for the checked state. To do this, get a copy of the ToggleButton style and in the Checked VisualState add this to the story board:

<ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="(UIElement.Content)">
 <DiscreteObjectKeyFrame KeyTime="0">
    <DiscreteObjectKeyFrame.Value>
         Save
    </DiscreteObjectKeyFrame.Value>
   </DiscreteObjectKeyFrame>
 </ObjectAnimationUsingKeyFrames>

If you'd rather not go down that route then you could add this to your Checked Triggers:

<ei:ChangePropertyAction PropertyName="ButtonText" Value="Save"/>

To use these approaches you will need a reference to the Microsoft.Expression.Interactions and System.Windows.Interactivity binaries from C:\Program Files (x86)\Microsoft SDKs\Expression\Blend.NETFramework\v4.5\Libraries.

Share:
57,484
Hasan Zubairi
Author by

Hasan Zubairi

Updated on July 13, 2022

Comments

  • Hasan Zubairi
    Hasan Zubairi almost 2 years

    I want to change the button content depending on the previous content click. For example, if its Add then it should change to Save, and if it is Save then it should change back to Add.

    I know how to change the content of a button. but how can one read the content to make a change?

  • rollsch
    rollsch over 7 years
    How do you get NotifyPropertyChanged() to be accessible? What do you have to implement?
  • Rhyous
    Rhyous over 7 years
    When I answered this, I was likely using a ViewModelBase with NotifyPropertyChanged already implemented. See Step 2 of this article: rhyous.com/2010/12/29/…
  • rollsch
    rollsch over 7 years
    Id recommend you show that in your answer, eg what namespaces you are using (really helpful to show using statements) and what you are extending.
  • santos
    santos over 5 years
    They are blend namespace aliases in the xaml for Microsoft.Expression.Interaction (ei) and System.Windows.Interactivity
  • Alexandru Dicu
    Alexandru Dicu over 5 years
    This is not how you show do this in WPF. Maybe in WinForms.