5

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?

ekolis
  • 6,270
  • 12
  • 50
  • 101

4 Answers4

2

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

have you tried...

public void btn_OnClick(object sender, RoutedEventArgs e)
{
    btn.Text = "Ginger";
}
fradsham
  • 131
  • 2
  • 6
  • Tried that now but I still get the second error I listed above. Actually I had to replace `btn` with a call to `FindControl` to look it up by name, but I still get that error. – ekolis May 17 '20 at 02:58
  • It seems that someone had this problem before in a [github issue](https://github.com/AvaloniaUI/Avalonia/issues/3515) – fradsham May 21 '20 at 01:52
  • Noepe, I still get the error, even when I change it to `Click="{Binding btn_OnClick}"`... – ekolis May 21 '20 at 04:34
1

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

Tấn Nguyên
  • 1,607
  • 4
  • 15
  • 25
0

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

btn.Content = "Ginger";
kurakura88
  • 2,185
  • 2
  • 12
  • 18