如何:从动态数据中获取信息

时间:2012-04-19 16:55:35

标签: c# wpf web-services xaml dynamic

这听起来可能很奇怪,但它相当简单。我在这个服务中有一个wcf webservice(restful)我设置了一些通用的“用户”字段,我设置的其中一个字段是每次添加用户时递增的userID。所有非常简单的东西。

然后我有一个消费的wpf应用程序。对于我在webservice中上面提到的每个用户,我动态地设置内容以在我的wpf应用程序中保存每个用户,这是在按钮点击时完成的,然后加载内容区域,其中组合框和文本块整齐排列(再次非常简单)。我持有它们的容器是GroupBox.Header,它包含userID和Textblock,它等于学生的名字和姓氏。

像这样:

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表示UserID。

所以这让我想到了问题......如果你在图像中注意到我也动态设置了一个按钮(上面的代码)。此按钮加载名为ThisUser :)的用户控件应用程序,但我遇到的问题是能够:

  

1)首先获取当前单击的GroupBox.Header(UserID)   学生(由达林斯解答,见下文)

     

2)将该UserID发送给usercontrol“ThisUser”

解释问题1):

查看上面的代码有一种方法可以“获取”已点击的用户的groubox.header(我的应用程序图像中的“查看”按钮)

问题2)

以某种方式从我当前的应用程序“发送”到用户控件“ThisUser”或“make available”以供usercontrol使用。

我希望这一切都很好,清晰,并且能够提供最好的答案,如果还有其他你想知道的(代码,解释),请不要犹豫。

在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 个答案:

答案 0 :(得分:1)

好吧,首先,我将StudentID放入btnFindStudent.Tag

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

触发ClickEvent后,您可以从标记中提取ID并将其发送到 ThisUser UserControl。

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

我只是在UserControl中创建一个名为“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;

试一试。 (: