Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# usercontrol的公共字符串没有显示任何内容?_C#_Wpf_Linq_Xaml_User Controls - Fatal编程技术网

C# usercontrol的公共字符串没有显示任何内容?

C# usercontrol的公共字符串没有显示任何内容?,c#,wpf,linq,xaml,user-controls,C#,Wpf,Linq,Xaml,User Controls,在my usercontrolViewUser中,groupbox标题和textblock没有显示用户ID 主窗口: private void btnGeneral_Click(object sender, RoutedEventArgs e) { ViewUser myusercontrol = new ViewUser(); String id = (String)((Button)sender).Tag; myusercontrol.UserID = id;

在my usercontrol
ViewUser
中,groupbox标题和textblock没有显示用户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;
    }
}

}

在设置UserID之前,您正在设置groupBox1.Header和textBlock1.Text。两种选择:

覆盖OnPreRender并在其中设置它们

直接从属性设置它们:

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

Kirsty,您应该在每次用户ID属性更改时更新GroupBox和TextBlock

public string UserID 
{ 
    get { return _userID; } 
    set
    {
        _userID = value;
        groupBox1.Header = _userID; 
        textBlock1.Text = _userID; 
    } 
} 
目前,您仅在OnInitialized中更新了一次GroupBox和TextBlock。但是OnInitialized只在ViewUser控件初始化后调用一次,以后不再调用


这就是n8wrl回答第二部分的意思。

base.OnPreRender(e);不存在?也没有适合onprerenderOh拍摄的方法-我想的是ASP.NET。不管怎样,我确信在控制生命周期中也有类似的东西。尝试“从属性设置”选项我严重困惑是否无法设置它,因此当我单击“查看”按钮时,它将根据字符串自动填充字段?