WPF C # Doubt - SetFocus in a Text and leave a button disabled

0

I am having difficulty in C # because when I click on a button, it has to disable a button and leave the focus in a txt. But I do not 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 -->
    
asked by anonymous 28.02.2018 / 13:25

1 answer

1

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

So, to disable the button and put the focus in the text field, you must change the Button_Click event that is in the code behind

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

It's important to remember that your XAML should reference the CS File: At the beginning of the XAML file you will find the declaration of the Window tag and its reference to the class. If the CS file does not find the elements declared in your XAML, that reference is probably missing.

See the example:

XAML file:

CSfile:

    
28.02.2018 / 15:31