信息這聽起來在第一,但其相當簡單奇怪。我在這個服務中有一個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);
}
}
和輸出是這樣的:
通知的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;
}
}
UserControl沒有任何內容,所以w我可以在按鈕上點擊字符串id,然後發送給用戶控件,或者我應該說你會怎麼做。所以用於Windows窗體,它很容易將數據從一個窗口發送到另一個窗口,但wpf真的使它非常難以做到。特別是當用戶控件仍在同一個窗口中時,你會認爲它更加容易。但是,感謝你的精彩標籤的想法! – 2012-04-19 17:19:56
我完全明白...從WinForms轉移到WPF很糟糕,因爲很多東西都是相似的,而且很多東西都不是。我會用更多信息更新我的答案。 – darin 2012-04-19 17:25:58
感謝Darin ......很高興至少有一個人明白了我的問題:) +1 – 2012-04-19 17:31:00