【问题标题】:Add parameter to Button click event为按钮单击事件添加参数
【发布时间】:2023-03-28 02:00:01
【问题描述】:

我有一个这样的 wpf 按钮:

<Button Click="button1_Click" Height="23" Margin="0,0,5,0" Name="button1" Width="75">Initiate</Button>

我想将 {Binding Code} 作为参数传递给 button1_click 处理程序。
我该怎么办?

免责声明:对 WPF 来说真的很新

【问题讨论】:

    标签: c# wpf .net-3.5 button click


    【解决方案1】:

    简单的解决方案:

    <Button Tag="{Binding Code}" ...>
    

    在您的处理程序中,将sender 对象转换为Button 并访问Tag 属性:

    var myValue = ((Button)sender).Tag;
    

    更优雅的解决方案是使用Command pattern of WPF:为您希望按钮执行的功能创建一个命令,将命令绑定到按钮的Command 属性并将CommandParameter 绑定到您的值。

    【讨论】:

    • 该标签现在可以使用,对于更复杂的场景,我将研究命令模式。谢谢
    【解决方案2】:

    我不太喜欢“标签”,所以也许

    <Button Click="button1_Click" myParam="parameter1" Height="23" Margin="0,0,5,0" Name="button1" Width="75">Initiate</Button>
    

    然后通过属性访问。

     void button1_Click(object sender, RoutedEventArgs e)
     {
        var button = sender as Button;
        var theValue = button.Attributes["myParam"].ToString()
     }
    

    【讨论】:

    • 成员“myParam”无法识别或无法访问。
    【解决方案3】:

    这有两种方法:

    转换 DataContext

     void button1_Click(object sender, RoutedEventArgs e)
     {
        var button = sender as Button;
        var code = ((Coupon)button.DataContext).Code;
     }
    

    或者使用作为通用状态属性的 Tag 属性

     <Button Click="button1_Click" Height="23" Margin="0,0,5,0" Name="button1" Tag="{Binding Code}" />
    

    然后

    void button1_Click(object sender, RoutedEventArgs e)
    {
        var button = sender as Button;
        var code = button.Tag;
    }
    

    【讨论】:

    • 我会使用(Button)sender 而不是sender as Button。在sender 不是按钮的不太可能的情况下,(Button)sender 将抛出 InvalidCastException,从而帮助您找到错误。另一方面,在这种情况下,您的代码会默默地将 button 设置为 null,稍后会导致 NullReferenceException。
    【解决方案4】:

    使用 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>
    

    还有MainPage Code Behing,此代码示例是使用页面类型作为参数导航到另一个页面,您需要在此处设置“YourPageTypeHere”和引用页面。

    然后在后面实现代码。

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

    在您的 App 类中还需要 MainPage 中的 NavigationPage 实例来导航(对于此示例)

    public App ()
    {
        InitializeComponent();
    
        MainPage = new NavigationPage(new MainPage());
    }
    

    适用于 xamarin 表单,但适用于 WPF 项目。

    可以使用 WPF 和 Xamarin 更改命令:“https://stackoverflow.com/a/47887715/8210755”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-18
      • 2012-09-01
      • 2013-05-02
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多