publiccontrol的公共字符串没有显示任何内容?

时间:2012-04-19 18:40:41

标签: c# wpf linq xaml user-controls

在我的usercontrol ViewUser中,groupbox标头和textblock不显示UserID?

主窗口:

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

}

2 个答案:

答案 0 :(得分:1)

Kirsty,你应该每次更新GroupBox和TextBlock 更改UserID属性:

public string UserID 
{ 
    get { return _userID; } 
    set
    {
        _userID = value;
        groupBox1.Header = _userID; 
        textBlock1.Text = _userID; 
    } 
} 

目前,您只在OnInitialized中更新了GroupBox和TextBlock一次。但OnInitialized只在ViewUser控件初始化后调用一次,而不会再次调用。

这就是n8wrl对他答案第二部分的意思。

答案 1 :(得分:-1)

您在设置UserID之前设置了groupBox1.Header和textBlock1.Text。两个选项:

覆盖OnPreRender并将其设置在那里。

直接从您的媒体资源中设置:

public string UserID
{
    get { return textBlock1.Text; }
    set
    {
        textBlock1.Text = value;
        groupBox1.Header = value;
    }
}