C# 根据上次点击更改按钮内容和文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10004422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Button Content and Text Based On Previous Click
提问by Hasan Zubairi
I want to change the button content depending on the previous content click. For example, if its Addthen it should change to Save, and if it is Savethen it should change back to Add.
我想根据之前的内容点击更改按钮内容。例如,如果是Add,则应更改为Save,如果是Save,则应更改回Add。
I know how to change the content of a button. but how can one readthe content to make a change?
我知道如何更改按钮的内容。但是如何阅读内容以进行更改?
采纳答案by Nikhil Agrawal
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.
获取或设置可用于存储有关此元素的自定义信息的任意对象值。
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";
}
}
回答by surfen
An alternate design could be if one created 2 buttons such as AddButtonand SaveButton, then one could show or hide them respectively (using Visibilityproperty).
另一种设计可能是,如果创建 2 个按钮,例如AddButton和SaveButton,则可以分别显示或隐藏它们(使用Visibility属性)。
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.
因为这是一个关注点分离的问题。例如,在单击处理程序中,您不需要检查您所处的模式,因为您将拥有单独的处理程序。您还希望按钮具有不同的图标、不同的工具提示等。
回答by Rhyous
If you are using MVVM, bind the content to a value and bind the command to function.
如果您使用的是 MVVM,请将内容绑定到一个值并将命令绑定到函数。
<Button Content="{Binding ButtonText}" Command="{Binding ButtonClickCommand}"/>
Of course, you then have String ButtonText and ButtonClickCommand as properties in your ViewModel.
当然,然后您将 String ButtonText 和 ButtonClickCommand 作为您的 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.
然后您可以让 ButtonClickCommand 更改属性,绑定会处理所有事情。
回答by santos
You can also use a ToggleButton with EventTriggers to execute the different methods for Checked and Unchecked states.
您还可以使用带有 EventTriggers 的 ToggleButton 来执行 Checked 和 Unchecked 状态的不同方法。
<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:
您还可以使用样式来修改 ToggleButton 模板并更改选中状态的文本。为此,获取 ToggleButton 样式的副本,然后在 Checked VisualState 中将其添加到故事板:
<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:
如果您不想沿着这条路线走,那么您可以将其添加到您的 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.
要使用这些方法,您需要引用来自 C:\Program Files (x86)\Microsoft SDKs\Expression\Blend.NETFramework\v4.5\Libraries 的 Microsoft.Expression.Interactions 和 System.Windows.Interactivity 二进制文件。
回答by Martin
I agree with Surfens answer that the question here is not a perfect example for a ToggleButtonbecause "Save" and "Add" a really different operations which should each have the own "ICommand" set on the respective button.
我同意 Surfens 的回答,这里的问题不是一个完美的例子,ToggleButton因为“保存”和“添加”是一个非常不同的操作,每个操作都应该在各自的按钮上设置自己的“ICommand”。
But here is some style that will change the content depending on the IsCheckedvalue of the ToggleButton.
但这里有一些样式会根据IsCheckedToggleButton的值更改内容。
The content will be "ValueForUnToggledState"if the button is not checked and change to "ValueForToggledState"when checked.
如果未选中按钮,则内容将为“ValueForUnToggledState”,选中时更改为“ValueForToggledState”。
<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.
这比其他一些答案更像 WPF。

