简体   繁体   中英

WPF Button content to Textbox

I'm trying to make a calculator and its my first time coding using anything with a GUI

I'm using Visual Studio 2012 WPF using C# application

How do i get the button content to the calculator textbox (screen) ?

XAML:

<Button x:Name="one" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" 
        Foreground="White" FontSize="20" FontWeight="Bold" Width="50" Height="41"
        Content="1" Margin="31,350,491,64" Click="button_Click"
        HorizontalAlignment="Left" VerticalAlignment="Bottom"/>

C#:

private void button_Click(object sender, EventArgs e)
{   
    Button b = (Button)sender;
    screen.Text = screen.Text + b.Text;
}

it underlines the Text next to the b.

Error 1 'System.Windows.Controls.Button' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'System.Windows.Controls.Button' could be found (are you missing a using directive or an assembly reference?)

Time to learn about MSDN! Here is the page for the Button class. You will notice there is no Text property, but there is a Content property. You will have to cast to string as Content is of type object .

screen.Text = screen.Text + b.Content.ToString();

Note that you could simplify this code to:

screen.Text += b.Content.ToString();

Use:

 screen.text=screen.Text +(String)b.Content

instead of:

b.Text 

in WPF the text of a button is accessed via Button.Content property

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM