【发布时间】:2016-10-05 21:26:51
【问题描述】:
我的问题很简单。
正如标题所述,我想知道如何将从 XAML 输入到 WPF TextBox 中的用户文本存储为代码隐藏中的字符串元素。其概念是用户在 TextBox 中输入文本,然后在单击“完成”按钮时,该 TextBox 中的值作为字符串元素存储在代码隐藏中(可以输入到数组中)。
XAML
<Window x:Class = "WpfApplication3.MainWindow"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local = "clr-namespace:WpfApplication3"
mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604">
<Grid Background = "White" Margin="0,4,0,-4">
<Grid.RowDefinitions>
<RowDefinition Height = "*" />
<RowDefinition Height = "*" />
<RowDefinition Height = "*" />
<RowDefinition Height = "*" />
<RowDefinition Height = "*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width = "*" />
</Grid.ColumnDefinitions>
<Label Content = "Employee Info" FontSize = "15"
FontWeight = "Bold" Grid.Column = "0" Grid.Row = "0"/>
<StackPanel Grid.Column = "0" Orientation = "Horizontal" Margin="0,38,0,26" Grid.RowSpan="2">
<Label Content = "Name" VerticalAlignment = "Center" Width = "70"/>
<TextBox Name = "txtName" Text = "Muhammad Ali" VerticalAlignment = "Center"
Width = "200">
</TextBox>
</StackPanel>
<StackPanel Grid.Column = "0" Grid.Row = "1" Orientation = "Horizontal" Margin="0,2,0,62" Grid.RowSpan="2">
<Label Content = "ID" VerticalAlignment = "Center" Width = "70"/>
<TextBox Name = "txtID" Text = "421" VerticalAlignment = "Center"
Width = "50">
</TextBox>
</StackPanel>
<StackPanel Grid.Column = "0" Grid.Row = "1" Orientation = "Horizontal" Margin="0,32" Grid.RowSpan="2">
<Label Content = "Age" VerticalAlignment = "Center" Width = "70"/>
<TextBox Name = "txtAge" Text = "32" VerticalAlignment = "Center"
Width = "50"></TextBox>
</StackPanel>
<StackPanel Grid.Column = "0" Grid.Row = "1" Orientation = "Horizontal" Margin="0,61,0,4" Grid.RowSpan="2">
<Label Content = "Position" VerticalAlignment = "Center" Width = "70"/>
<TextBox Name = "txtPosition" Text = "Programmer" VerticalAlignment = "Center"
Width = "200"></TextBox>
</StackPanel>
<Button Click ="Button_Click" x:Name="button" Content="Finished" HorizontalAlignment="Left" Margin="172,21,0,0" Grid.Row="4" VerticalAlignment="Top" Width="237"/>
</Grid>
代码隐藏
namespace WpfApplication3
{
public partial class MainWindow : Window
string EmpName = "blank";
string EmpID = "blank";
string EmpAge = "blank";
string EmpPosition = "blank";
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
txtName = EmpName; //Here's where I attempt to store the TextBox Information
txtID = EmpID;
txtAge = EmpAge;
txtPosition = EmpPosition;
}
}
}
感谢观看!
【问题讨论】:
-
你试过txtName.Text = EmpName吗?目前您正在更改整个文本框控件,而不仅仅是其中的文本。要将数据从 gui 读取到您的代码中,只需交换这两者即可得到 EmpName = txtName.Text。可能你应该看看 assignments 和 Properties 在 C# 中是如何工作的,这可以帮助你理解。
-
啊,所以必须颠倒过来。很简单!非常感谢!
-
或者,只需将文本框绑定到您要使用的属性。了解了赋值和属性的基础知识后,您可能希望查找“WPF 中的 MVVM”。
标签: c# wpf string xaml code-behind