AvaloniaUI –Access controls in CodeBehind

Veröffentlicht von

How to use controls like a TextBox in AvaloniaUI Code behind?

In my current project, I am using AvaloniaUI. I wanted to add some simple code to set the focus on a TextBox. So I gave it a name, just like in WPF.

<TextBox Width="400"  
x:Name="TextBoxTitle" 
Text="{Binding Title}" 
HorizontalAlignment="Left"></TextBox> 

The IDE recognized the name, at least it seamed that way, however the control was always null.

The solution was to use the “FindControl” method to get a reference to the control.

this.FindControl<TextBox>("TextBoxTitle").Focus(); 

Kommentar hinterlassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert