Change Button name via WPF code?

0
  

I want to access a control in WPF to access properties   modify it.

     

However, I can not seem to find much of an approach to Wpf today.

     

What I want to do for example is to change the name of a button to   any name.

XAML

<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="362,31,0,0" Height="20" Click="Button_Click" RenderTransformOrigin="0.5,0.5">
        <Button.RenderTransform>
            <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform Angle="360.279"/>
                <TranslateTransform/>
            </TransformGroup>
</Button.RenderTransform>

In XAML.CS, I do not know how to access the Button.

    
asked by anonymous 04.03.2018 / 20:10

1 answer

0

This is not the best way to do this (the correct one would be using Binding , but it is not the focus of the question), but here it is:

Set the name of your button in the file .xaml of your Window through the x:Name attribute

<Button x:Name="btnMyButton" Content="OK" Width="200" Height="30" ></Button>

With this, you can access it through the .xaml.cs file of your Window by calling the button by the previously created name btnMyButton :

public MainWindow()
{
    InitializeComponent();

    // Altera o conteúdo do botão
    btnMyButton.Content = "Clique aqui";

    // Altera o comprimento do botão
    btnMyButton.Width = 100;

    // Altera a altura do botão
    btnMyButton.Height = 30;

    // Define um evento ao clicar no botão
    btnMyButton.Click += BtnMyButton_Click;
}

private void BtnMyButton_Click(object sender, RoutedEventArgs e)
{
    throw new NotImplementedException();
}
    
05.03.2018 / 01:52