Change Button name via WPF code?

Asked

Viewed 170 times

0

I’m looking to access a control in WPF to access the properties of it and modify.

However, I can’t find much approach to Wpf currently.

What I want to do, for example, is change the name of a button for 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>

At XAML.CS, I don’t know how to access Button.

  • Include your code

  • @Leandroangelo ok.

1 answer

0


This is not the most appropriate way to do this (the correct 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 his Window through the attribute x:Name

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

With this, you can access it through the file .xaml.cs of his Window calling the button by the name previously created 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();
}

Browser other questions tagged

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