سؤال

Overview

I have a WPF application written in C# .NET 4.0. There are a number of buttons in this project. Currently, every EventHandler for the button click events calls the same method, with a different valued parameter. As this is all done in C# code, it is rather unwieldy and involves a lot of copy/paste.

I'd much rather use a Command, rather than a bunch of EventHandlers, as it keeps my code cleaner.

Problem

No matter what I do, I cannot get the XAML to accept the new command:

<Window.CommandBindings>
    <CommandBinding Command="ButtonClick" Executed="ButtonClick_Executed" />
</Window.CommandBindings>

The above code has an error: "CommandConverter cannot convert from System.String". This error exists on the Command="ButtonClick".

Research/Attempts

I've tried everything I can think of to make this work to no avail. I've looked at at least 30 different blog posts/tutorials/etc. on how to properly do this, so I don't have an actual list for you. I saw some people using the Binding keyword, so I attempted: Command="{Binding ButtonClick}", but apparently that's not allowed inside a CommandBinding section. I have also tried Command="{x:Static ButtonClick}", but that doesn't work either.

As for the "ButtonClick" itself, I have tried several possible values there. "ButtonClick" is an arbitrary text string that does not correspond to any class/method/variable. My original understanding of this parameter was for the value to be simply an identifying string. I have also tried using an actual method in the same class "ButtonClick_Executed". I have also tried using a class name which implements ICommand (Commands), as well as a method inside that class (Commands.ButtonClick_Executed).

For reference, I also tried doing this in the codebehind as per WPF: Binding to commands in code behind, but I couldn't make that work either.

Code

Code is abridged to what is relevant. From what I understand, the Button's Command parameter needs to match the CommandBinding's Command value. I have not added this in yet, since I can't even get the CommandBinding to work. All code is in the same namespace, in a single project.

MainWindow.xaml:

<Window x:Class="T9Messager.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="600" Width="540">
    <Window.CommandBindings>
        <CommandBinding Command="ButtonClick" Executed="ButtonClick_Executed" />
    </Window.CommandBindings>
    <Grid Margin="0,0,0,0">
        <Button x:Name="Button1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="165" Height="113">
            <TextBlock HorizontalAlignment="Center" TextAlignment="Center" FontSize="18" FontWeight="Bold">1</TextBlock>
        </Button>
        ... More buttons ...
    </Grid>
</Window>

MainWindow.xaml.cs:

public partial class MainWindow : Window, IObserver<Model>
{
    private Controller controller;

    private IDisposable Unsubscriber;

    public MainWindow()
    {
        Model model = new Model();
        Controller controller = new Controller(model);
        this.controller = controller; // MainWindow view = new MainWindow(c);
        Unsubscriber = model.Subscribe(this);

        InitializeComponent();
    }

    // This is the method I want to run
    public void ButtonClick_Executed(object sender, EventArgs ev)
    {
        Console.WriteLine("Command Executed");
    }
    // I want to avoid having a bunch of these
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        controller.HandleButtonClick('1');
    }

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        controller.HandleButtonClick('2');
    }

    ... More ButtonClick Events ...

    public void OnCompleted()
    {
        Unsubscriber.Dispose();
    }

    public void OnError(Exception error)
    {
        throw new NotImplementedException();
    }

    public void OnNext(Model value)
    {
        tb_text.Text = value.text;
    }
}

Commands.cs:

public class Commands : ICommand
{
    public Commands()
    {
        CommandBinding ButtonClickBinding = new CommandBinding(ButtonClickCommand);
        CommandManager.RegisterClassCommandBinding(typeof(Commands), ButtonClickBinding);
    }

    private RoutedUICommand ButtonClick = new RoutedUICommand("ButtonClick", "ButtonClick", typeof(Commands));

    public RoutedCommand ButtonClickCommand
    {
        get { return ButtonClick; }
    }

    // Getting any of the following 3 methods to execute would be fine
    public static void test()
    {
    }

    public void Execute(object parameter)
    {
        ButtonClick_Executed(null, null);
    }

    public void ButtonClick_Executed(object sender, EventArgs ev)
    {
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

}

Any and all help is greatly appreciated. Please let me know if you require any additional information.

Update I have attempted the answer proposed by Herdo. The new error I got was The namespace prefix "T9Messager" is not defined. After research, my XAML now opens like this:

<Window x:Class="T9Messager.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:T9Messager="clr-namespace:T9Messager"
    Title="MainWindow" Height="600" Width="540">

This seems to have fixed that particular issue, but now it looks as if the XAML is completely ignoring the Command parameter. Command="T9Messager:Commands.ButtonClick" creates the error Value cannot be null. Parameter name: value.

هل كانت مفيدة؟

المحلول

This ButtonClick is a String:

<CommandBinding Command="ButtonClick" Executed="ButtonClick_Executed" />

You need to write it as a member of the class (note that you need to add the namespace in the window declaration):

<Window xmlns:t9m="clr-namespace:T9Messager"
        ...>

<CommandBinding Command="t9m:Commands.ButtonClick"
                Executed="ButtonClick_Executed" />

نصائح أخرى

In my code, I removed the "x:Static" from 'Command="{x:Static ButtonClick}"' and worked.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top