ekolis
ekolis

Reputation: 6796

Avalonia button click event is not working

I have a simple Avalonia form:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="AvaloniaExperiment.MainWindow"
        Title="AvaloniaExperiment">
  <StackPanel>
    <TextBlock>Welcome to Avalonia!</TextBlock>
    <Button Name="btn" Click="btn_OnClick">Fred!</Button>
  </StackPanel>
</Window>

And a method in the code behind (I want to do things this way until I become familiar with Avalonia, then maybe I'll try MVVM):

private void btn_OnClick()
{
    btn.Text = "Ginger";
}

However I get these compile errors:

The name btn does not exist in the current context (in the code behind)

Unable to find suitable setter or adder for property Click of type Avalonia.Controls:Avalonia.Controls.Button for argument System.Private.CoreLib:System.String, available setter parameter lists are: System.EventHandler`1[[Avalonia.Interactivity.RoutedEventArgs, Avalonia.Interactivity, Version=0.9.0.0, Culture=neutral, PublicKeyToken=null]] (in the XAML)

Unable to find suitable setter or adder for property Command of type Avalonia.Controls:Avalonia.Controls.Button for argument System.Runtime:System.String, available setter parameter lists are: Avalonia.UnsetValueType Avalonia.Data.IBinding System.Windows.Input.ICommand (also in the XAML)

What could I be doing wrong in hooking up this event handler?

Upvotes: 5

Views: 11481

Answers (4)

Stefan Smietanowski
Stefan Smietanowski

Reputation: 146

The sender is the button you just clicked, so typecast sender to Button and set its Content property (not Text) to whatever you want to.

public void btn_OnClick( object? sender, RoutedEventArgs args )
{
    ( sender as Button )!.Content = "Ginger";
}

No need to look it up in the tree or anything else, this way you can reuse the same code behind for all your buttons, and for instance, depending on which button it is, set different names or styles, or other properties, etc.

More advanced:

public void btn_OnClick( object? sender, RoutedEventArgs args )
{
    var button = ( sender as Button )!;
    switch ( button.Name )
    {
        case "btn":
        {
            button.Content = "Ginger";
        }
        break;
        case "otherBtn":
        {
            button.Content = "Ale";
        }
        break;
        default:
        {
            button.Content = "No clue which Button you are!";
        }
        break;
    }
}

Upvotes: 3

xdtssm
xdtssm

Reputation: 11

You should add a ControlLink in the Parent Control Constructor like this:

public class AnyParentControl
{
    Button btn; // for the class
    public AnyParentControl() // constructor
    {
        InitializeComponent(); // necessary method for Avalonia

        btn = this.Find<Button>("The Control Name on XAML File");
        btn.Click += Cbtn_Click; // event link
    }
}

Greetings from Peru :D

Upvotes: 1

fradsham
fradsham

Reputation: 141

have you tried...

public void btn_OnClick(object sender, RoutedEventArgs e)
{
    btn.Text = "Ginger";
}

Upvotes: 1

kurakura88
kurakura88

Reputation: 2305

Button does not have Text property. It does have Content.

btn.Content = "Ginger";

Upvotes: 0

Related Questions