【问题标题】:WPF Button content to TextboxWPF 按钮内容到文本框
【发布时间】:2014-10-22 21:57:13
【问题描述】:

我正在尝试制作一个计算器,这是我第一次使用带有 GUI 的任何东西进行编码

我正在通过 C# 应用程序使用 Visual Studio 2012 WPF

如何将按钮内容获取到计算器文本框(屏幕)?

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;
}

它在 b 旁边的文本下划线。

错误 1“System.Windows.Controls.Button”不包含“Text”的定义,并且找不到接受“System.Windows.Controls.Button”类型的第一个参数的扩展方法“Text”(是您缺少 using 指令或程序集引用?)

【问题讨论】:

  • 有些不对劲?应该是void button_Click(object sender, RoutedEventArgs e) 吗?

标签: c# wpf xaml button textbox


【解决方案1】:

是时候了解 MSDN 了!这是page 类的Button。您会注意到没有Text 属性,但有Content 属性。您必须转换为string,因为Content 的类型为object

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

请注意,您可以将此代码简化为:

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

【讨论】:

    【解决方案2】:

    用途:

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

    代替:

    b.Text 
    

    在 WPF 中,按钮的文本通过以下方式访问 Button.Content属性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多