2012-04-19 44 views
1

信息这听起来在第一,但其相当简单奇怪。我在这个服务中有一个wcf webservice(restful),我设置了一些通用的“用户”字段,我设置的字段之一是每次添加用户时增加的用户id。所有非常简单的东西。如何:从动态数据

我再有一个消费WPF应用程序。对于我在web服务中提到的每个用户,我都会动态地设置内容来保存我的wpf应用程序中的每个用户,这些用户是通过点击按钮完成的,然后加载内容区域,并且整齐排列(再次非常简单)。我把它们放在容器中的容器是GroupBox.Header,它包含用户ID和文本块,它等于学生的名字和姓氏。

像这样:

private void button1_Click(object sender, RoutedEventArgs e) 
     { 

      if (string.IsNullOrEmpty(textBox1.Text)) 
      { 
       PanelMainContent.Children.Clear(); 
       MainArea1.Children.Clear(); 
       string uriGroups = "http://localhost:8000/Service/Student"; 
       XDocument xDoc = XDocument.Load(uriGroups); 
       var sortedXdoc = xDoc.Descendants("Student") 
           .OrderByDescending(x => Convert.ToDateTime(x.Element("TimeAdded").Value)); 
       foreach (var node in xDoc.Descendants("Student").OrderByDescending(x => Convert.ToDateTime(x.Element("TimeAdded").Value))) 
       { 

        GroupBox groupbox = new GroupBox(); 
        groupbox.Header = String.Format(node.Element("StudentID").Value); 
        App.Current.Properties["groupboxHeader"] = groupbox.Header; 
        groupbox.Width = 100; 
        groupbox.Height = 100; 
        groupbox.Margin = new Thickness(1); 

        Button btnFindStudent = new Button(); 
        btnFindStudent.Click += this.btnGeneral_Click; 
        btnFindStudent.Name = Convert.ToString("btnViewStudent"); 
        btnFindStudent.Tag = Convert.ToString("ViewStudent"); 
        btnFindStudent.Content = Convert.ToString("View"); 
        btnFindStudent.HorizontalAlignment = HorizontalAlignment.Right; 
        btnFindStudent.Height = 20; 
        btnFindStudent.Width = 36; 

        TextBlock textBlock = new TextBlock(); 
        textBlock.Text = String.Format(node.Element("FirstName").Value + " " + (node.Element("LastName").Value)); 
        textBlock.TextAlignment = TextAlignment.Center; 

        TextBlock textBlock1 = new TextBlock(); 
        textBlock1.Text = (DateTime.Parse(node.Element("TimeAdded").Value)).ToString("d"); 
        String.Format("{0:d/M/yyyy}", DateTime.Parse(node.Element("TimeAdded").Value)); 
        textBlock1.TextAlignment = TextAlignment.Center; 
        textBlock1.VerticalAlignment = VerticalAlignment.Bottom; 

        StackPanel stackPanel = new StackPanel(); 
        stackPanel.Children.Add(groupbox); 
        stackPanel.Children.Add(btnFindStudent); 
        stackPanel.Children.Add(textBlock); 
        stackPanel.Children.Add(textBlock1); 
        stackPanel.Margin = new Thickness(5); 
        stackPanel.MouseEnter += new MouseEventHandler(stackpanel_MouseEnter); 
        stackPanel.MouseLeave += new MouseEventHandler(stackpanel_MouseLeave); 
        MainArea1.Children.Add(stackPanel); 
       } 
      } 

和输出是这样的:

enter image description here

通知的GroupBox.header美国的用户名。

因此,这使我的问题......如果您发现图像中我也动态地设置(上面的代码)按钮。此按钮加载一个名为ThisUser一个usercontrol applie :)但我有是能够的问题:

1)首先获得GroupBox.Header(用户名)的当前点击 学生(由Darins解决下面回答)

2)发送用户ID即到该用户控件 “ThisUser”

为了解释的问题1):

噜在上面的代码国王是有办法的,我可以“获取”谁已被点击(我的应用程序的图像中的“查看”按钮)

而对于问题2)用户的groubox.header

不知怎的,从我的当前应用到该用户控件“ThisUser”或“使可用”的用户控件来再消费“发送”。

我希望这一直不错,清晰,奠定了很好地提供最佳的答案,如果有其他任何你想知道(代码说明)请不要犹豫,问。

代码发送的btnGeneralClick(查看按钮)数据:

public FindStudent() 
    { 
     InitializeComponent(); 
    } 
    private void btnGeneral_Click(object sender, RoutedEventArgs e) 
    { 

     ViewStudent myusercontrol = new ViewStudent(); 
     String id = (String)((Button)sender).Tag; 
     myusercontrol.StudentID = id; 
     PanelMainContent.Children.Add(myusercontrol); 

    } 

用户控件:

public partial class ViewStudent : UserControl 
{ 

    public ViewStudent() 
    { 
     InitializeComponent(); 

    } 
    private string _studentID; 

    public string StudentID 
    { 
     get 
     { 
      return _studentID; 
     } 
     set 
     { 
      _studentID = value; 
     } 
    } 
    protected override void OnInitialized(EventArgs e) 
    { 
     base.OnInitialized(e); 
     groupBox1.Header = StudentID; 
     //or 
     textBlock1.Text = StudentID; 
    } 
} 

回答

1

嗯,首先,我把StudentID在btnFindStudent.Tag

btnFindStudent.Tag = node.Element("StudentID").Value.ToString(); 

当ClickEvent被触发,你可以拉出来的ID标签,并把它送上你ThisUser UserControl。

String id = (String)((Button)sender).Tag; 

我刚刚创建一个名为“StudentID”的用户控件的公共属性,并设置它。没有看到你的UserControl很难说。 :(

更多信息:

一下添加到ViewStudent:

#region StudentID 

public static readonly DependencyProperty StudentIDProperty = DependencyProperty.Register("StudentID", typeof(String), typeof(ViewStudent), new PropertyMetadata(OnStudentIDChanged)); 

public string StudentID 
{ 
    get { return (string)GetValue(StudentIDProperty); } 
    set { SetValue(StudentIDProperty, value); } 
} 

static void OnStudentIDChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    (d as ViewStudent).OnStudentIDChanged(e); 
} 

void OnStudentIDChanged(DependencyPropertyChangedEventArgs e) 
{ 
    groupBox1.Header = StudentID; 
    textBlock1.Text = StudentID; 
} 

#endregion 

,摆脱其他StudentID属性我们前面提出的

ViewStudent myUserControl = new ViewStudent(); 
myUserControl.StudentID = (String)((Button)sender).Tag; 

把那一个镜头。(:

+0

UserControl没有任何内容,所以w我可以在按钮上点击字符串id,然后发送给用户控件,或者我应该说你会怎么做。所以用于Windows窗体,它很容易将数据从一个窗口发送到另一个窗口,但wpf真的使它非常难以做到。特别是当用户控件仍在同一个窗口中时,你会认为它更加容易。但是,感谢你的精彩标签的想法! – 2012-04-19 17:19:56

+1

我完全明白...从WinForms转移到WPF很糟糕,因为很多东西都是相似的,而且很多东西都不是。我会用更多信息更新我的答案。 – darin 2012-04-19 17:25:58

+0

感谢Darin ......很高兴至少有一个人明白了我的问题:) +1 – 2012-04-19 17:31:00