C# 给按钮点击事件添加参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2006507/
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
Add parameter to Button click event
提问by Boris Callens
I have a wpf button like this:
我有一个像这样的 wpf 按钮:
<Button Click="button1_Click" Height="23" Margin="0,0,5,0" Name="button1" Width="75">Initiate</Button>
And I want to pass {Binding Code}
passed as parameter to the button1_click handler.
How do I go about this?
我想将{Binding Code}
作为参数传递给 button1_click 处理程序。
我该怎么做?
Disclaimer: really new to WPF
免责声明:WPF 的新手
采纳答案by Heinzi
Simple solution:
简单的解决方案:
<Button Tag="{Binding Code}" ...>
In your handler, cast the sender
object to Button
and access the Tag
property:
在您的处理程序中,将sender
对象强制转换为Button
并访问该Tag
属性:
var myValue = ((Button)sender).Tag;
A more elegant solution would be to use the Command pattern of WPF: Create a Command for the functionality you want the button to perform, bind the Command to the Button's Command
property and bind the CommandParameter
to your value.
一个更优雅的解决方案是使用WPF的命令模式:为您希望按钮执行的功能创建一个命令,将命令绑定到按钮的Command
属性并将 绑定CommandParameter
到您的值。
回答by bendewey
Well there are two ways of doing this:
那么有两种方法可以做到这一点:
Cast the DataContext
投射数据上下文
void button1_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
var code = ((Coupon)button.DataContext).Code;
}
Or use the Tag property which is a generic state property
或者使用 Tag 属性,这是一个通用的状态属性
<Button Click="button1_Click" Height="23" Margin="0,0,5,0" Name="button1" Tag="{Binding Code}" />
then
然后
void button1_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
var code = button.Tag;
}
回答by TheMonkeyMan
I'm not overly a fan of 'Tag' so perhaps
我不太喜欢“标签”,所以也许
<Button Click="button1_Click" myParam="parameter1" Height="23" Margin="0,0,5,0" Name="button1" Width="75">Initiate</Button>
Then accessing via the Attributes.
然后通过属性访问。
void button1_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
var theValue = button.Attributes["myParam"].ToString()
}
回答by Juan Pablo
Using Xaml and DataContext
使用 Xaml 和 DataContext
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:DataAndCloudServices"
x:Class="DataAndCloudServices.MainPage" >
<StackLayout>
<!-- Command Implemented In Code Behing -->
<Button Text="Consuming Web Services Samples"
Command="{Binding NavigateCommand}"
CommandParameter="{x:Type local:YourPageTypeHere}" >
</Button>
</StackLayout>
</ContentPage>
And MainPage Code Behing, this code example is to navigate to another page using the page type as argument, you need to make "YourPageTypeHere" and reference page here.
和MainPage Code Behing,这个代码示例是使用页面类型作为参数导航到另一个页面,您需要在此处制作“YourPageTypeHere”和引用页面。
Then implement code Behind.
然后在后面实现代码。
using System;
using System.Windows.Input;
using Xamarin.Forms;
namespace DataAndCloudServices
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
NavigateCommand = new Command<Type>(
async (Type pageType) =>
{
Page page = (Page)Activator.CreateInstance(pageType);
await Navigation.PushAsync(page);
});
this.BindingContext = this;
}
public ICommand NavigateCommand { private set; get; }
}
}
Also in your App class need a Instance of NavigationPage in MainPage to Navigate (For this example)
同样在您的 App 类中需要 MainPage 中的 NavigationPage 实例来导航(对于此示例)
public App ()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage());
}
It is for xamarin forms, But It is similar for WPF Projects.
它适用于 xamarin 形式,但与 WPF 项目类似。
Command could be changed with for WPF and Xamarin: "https://stackoverflow.com/a/47887715/8210755"
可以使用 WPF 和 Xamarin 更改命令:“ https://stackoverflow.com/a/47887715/8210755”