不显示任何内容的用户控件的公共字符串
本文关键字:字符串 控件 用户 任何内 显示 | 更新日期: 2023-09-27 18:31:36
在我的用户控件中,ViewUser
组框标题和文本块没有显示用户ID?
主窗口:
private void btnGeneral_Click(object sender, RoutedEventArgs e)
{
ViewUser myusercontrol = new ViewUser();
String id = (String)((Button)sender).Tag;
myusercontrol.UserID = id;
PanelMainContent.Children.Add(myusercontrol);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
string uriUsers = "http://localhost:8000/Service/User";
XDocument xDoc = XDocument.Load(uriUsers);
var sortedXdoc = xDoc.Descendants("User")
.OrderByDescending(x => Convert.ToDateTime(x.Element("TimeAdded").Value));
foreach (var node in xDoc.Descendants("User"))
{
Button btnFindStudent = new Button();
btnUser.Click += this.btnGeneral_Click;
btnUser.Tag = String.Format(node.Element("UserID").Value);
//also tryed btnUser.Tag = node.Element("UserID").Value;
用户控件:
public partial class ViewUser : UserControl
{
public ViewUser()
{
InitializeComponent();
}
private string _user;
public string UserID
{
get { return _userID; }
set { _userID = value; }
}
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
groupBox1.Header = UserID;
textBlock1.Text = UserID;
}
}
}
Kirsty,每次 UserID 属性更改时,都应更新 GroupBox 和 TextBlock:
public string UserID
{
get { return _userID; }
set
{
_userID = value;
groupBox1.Header = _userID;
textBlock1.Text = _userID;
}
}
目前,您只在 OnInitialized 中更新一次 GroupBox 和 TextBlock。但是 OnInitialized 仅在 ViewUser 控件初始化后调用一次,并且永远不会再次调用。
这就是 n8wrl 在他回答的第二部分的意思。
您在设置 UserID 之前设置 groupBox1.Header 和 textBlock1.Text。两个选项:
覆盖 OnPreRender 并将它们设置在那里。
或
直接从您的媒体资源设置它们:
public string UserID
{
get { return textBlock1.Text; }
set
{
textBlock1.Text = value;
groupBox1.Header = value;
}
}