有一个带文本框的简单WPF窗口。在这个窗口中,我想显示一个userId。在winforms中这是一个如此简单的过程,但我似乎无法在XAML中绕过它。 代码是一个简单的类 - 用户,列表如下......
public class User
{
private string userId;
private string employeeNumber;
...
public User()
{ }
public string UserId
{
get
{
return userId;
}
set
{
userId = value;
}
}
public string EmployeeNumber
{
get
{
return employeeNumber;
}
set
{
employeeNumber = value;
}
}
}
...
wpf窗口的xaml是......
<Window
...
<Window.Resources></Window.Resources>
<Grid>
<Label x:Name="lblUserId" Content="User Id" HorizontalAlignment="Left" Margin="84,76,0,0" VerticalAlignment="Top"/>
<Label x:Name="lblEmployeeNumber" Content="Employee Number" HorizontalAlignment="Left" Margin="84,106,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="tbUserId" HorizontalAlignment="Left" Height="23" Margin="200,76,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" />
<TextBox x:Name="tbEmployeeNumber" HorizontalAlignment="Left" Height="23" Margin="200,106,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" />
</Grid>
</Window>
当窗口加载时,执行后面的代码。
public partial class winTest : Window
{
public winTest()
{
InitializeComponent();
}
private User user;
private List<User> userList;
private List<Permissions> permissionsList;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
string userId = "1234";
user = UserDB.GetUserInfo(userId);
}
}
...
GetUserInfo的代码是
public static User GetUserInfo(string userID)
{
User user = new User();
SqlConnection connection = BusinessDB.GetConnection();
string selectStatement = "SELECT userid, empno, FROM employees " +
"WHERE userid = @UserID";
SqlCommand selectCommand = new SqlCommand(selectStatement, connection);
selectCommand.Parameters.AddWithValue("userid", userID);
try
{
connection.Open();
//SqlDataReader reader = selectCommand.ExecuteReader();
SqlDataReader reader = selectCommand.ExecuteReader(CommandBehavior.SingleRow);
if (reader.Read())
{
if (reader["userid"] == DBNull.Value)
{
user.UserId = "";
}
else
{
user.UserId = (reader["userid"]).ToString();
}
if (reader["empno"] == DBNull.Value)
{
user.EmployeeNumber = "";
}
else
{
user.EmployeeNumber = reader["empno"].ToString();
}
}
return user
}
...
单步执行代码会发现用户确实填充了正确的信息 - 它是获取我的绑定部分。上面的xaml没有列出任何绑定,因为我尝试将它绑定到除了我的狗鼻子之外的所有东西,我仍然得到空文本框。 有人可以帮助一个wpf新手吗?非常感谢提前。
答案 0 :(得分:0)
您需要将User
设为公共财产,而非私人字段:
public User User { get; set }
如果您希望绑定响应User
值的更改,则您的Window
类需要实现INotifyPropertyChanged
。
public partial class winTest : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
// <your other winTest code goes here>
}
您的绑定应如下所示:
<TextBox x:Name="tbUserId"
Text="{Binding User.UserId}"
HorizontalAlignment="Left"
Height="23"
Margin="200,76,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="120" />
答案 1 :(得分:0)
正如devuxer所说,请确保将您的公共属性重写为:
public string UserId { get; set; }
public string EmployeeNumber { get; set; }
在Window_loaded();
上尝试此方法private void Window_Loaded(object sender, RoutedEventArgs e)
{
string userId = "1234";
user = UserDB.GetUserInfo(userId);
tbUserId.Text = user.UserId;
tbEmployeeNumber.Text = user.tbEmployeeNumber;
}