2012-04-19 50 views
0

在我的usercontrol ViewUser groupbox标题和文本块不显示用户ID?usercontrol的公共字符串不显示任何内容?

主窗口:

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

}

回答

1

科斯蒂,你应该每时间UserID属性更改更新组框和TextBlock的

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

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

这就是n8wrl与他答案的第二部分的含义。

-1

你的用户ID之前设置groupBox1.Header和textBlock1.Text被设定。两个选项:

重写OnPreRender并将它们设置在那里。

直接从您的属性设置它们:

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

base.OnPreRender(e);不存在? – 2012-04-19 18:51:44

+0

并没有适合onprerender的方法 – 2012-04-19 18:58:04

+0

哦拍 - WPF我在想ASP.NET。无论如何,我确信控制生命周期中有类似的东西。尝试设置从属性选项 – n8wrl 2012-04-19 18:59:20