This might sound strange at first but its rather simple. I have a wcf webservice (restful) in this service I have set some generic “user” fields and one of the fields which I have set is the userID to increment every time a user is added. All pretty simple stuff.
I then have a consuming wpf application. For each of the users mentioned above in my webservice I dynamically set out content to hold each of those users in my wpf application which is done on button click, this then loads the content area with groupboxs and textblocks neatly arranged (again pretty simple). The containers I hold them in are GroupBox.Header which holds the userID and Textblock which equals the students first name and last name.
Like so:
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);
}
}
And the output looks like this:

Notice the GroupBox.header states the UserID.
So this brings me to the problem… if you notice in the image I also dynamically set a button (code above). This button loads a usercontrol applie named ThisUser 🙂 but the problem I am having is being able to:
1) First get GroupBox.Header(UserID) of the currently clicked
student (SOLVED by Darins answer below)2) Send that UserID to the usercontrol “ThisUser”
To explain question 1):
Looking at the code above is there a way in which I can “Get” the groubox.header of that user who has been clicked (“view” button in the image of my app)
And for question 2)
Somehow “send” from my current app to the usercontrol “ThisUser” or “make available” for the usercontrol to then consume.
I hope this has been nice and clear and layed out well to provide the best possible answer, if there is anything else you wish to know (code, explanation) please dont hesitate to ask.
Code to send data on btnGeneralClick (view button):
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);
}
UserControl:
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;
}
}
Well, first, I’d put the StudentID in the btnFindStudent.Tag
When the ClickEvent is fired, you can pull the ID out of the Tag and send it off to your ThisUser UserControl.
I’d just create a public property in the UserControl called “StudentID” and set it there. It’s hard to say without seeing your UserControl. 🙁
More Information:
Add this to ViewStudent:
And get rid of that other StudentID Property we made earlier.
Give that a shot. (: