Doubt WPF C# - Setfocus on a Text and leave a button disabled

Asked

Viewed 182 times

0

I’m having difficulty in c# because when I click a button, it has to disable a button and leave the Focus in a txt.
But I don’t know how to reference an object in wpf (xaml) in the encoding file (Cs)

  private void View(object parameter)
  {
    //Como referênciar o button01 e o txt01 aqui?
  }
<Button HorizontalAlignment="Center"
        Command="{Binding SearchCommand, Mode=OneTime}"
        Visibility="{Binding ShowList}"
        Style="{StaticResource ButtonProcurar}" Click="Button_Click"/>
        
<Button x:Name="button01" Content="Desabilitar ao clicar no botão acima" />

<TextBox x:Name="txt01" Text="Texto que o cursor deverá vir ao clicar no primeiro botão" />
        <!-- C# WPF XAML -->

1 answer

1


You access the XAML elements by the same name given to the element in the property x:Name. In this case the element names are button01, txt01.

Therefore, to disable the button and put the focus in the text field, you must change the Button_click event that is in code Behind (XAML Cs file)

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        button01.Enabled = false;
        txt01.Focus();
    }

It is important to remember that your XAML should reference the CS file: At the beginning of the XAML file you will find the Window tag declaration and the respective reference to the class. If the CS file does not find the declared elements in your XAML, it is possibly missing this reference.

Take the example:

XAML:

Exemplo

CS file:

ExemploCS

  • Thank you so much for your help !!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.