所以我有一些学生记录,它们从后面的代码中添加内容,我试图将字符串从一个用户控件传递到另一个用户控件:
private Dictionary<string, UserControl> _userControls = new Dictionary<string, UserControl>();
public Dictionary<string, UserControl> GetUserControls()
{
return _userControls;
}
public FindStudent()
{
InitializeComponent();
List<string> userControlKeys = new List<string>();
userControlKeys.Add("ViewStudent");
Type type = this.GetType();
Assembly assembly = type.Assembly;
foreach (string userControlKey in userControlKeys)
{
string userControlFullName = String.Format("{0}.{1}", type.Namespace, userControlKey);
UserControl userControl = (UserControl)assembly.CreateInstance(userControlFullName);
_userControls.Add(userControlKey, userControl);
}
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;
// when button is clicked it should get the header which is set to studentID
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;
StackPanel stackPanel = new StackPanel();
stackPanel.Children.Add(groupbox);
stackPanel.Children.Add(btnFindStudent);
stackPanel.Margin = new Thickness(5);
MainArea1.Children.Add(stackPanel);
}
}
private void btnGeneral_Click(object sender, RoutedEventArgs e)
{
PanelMainContent.Children.Clear();
Button button = (Button)e.OriginalSource;
//PanelMainWrapper.Header = button.Content;
Type type = this.GetType();
Assembly assembly = type.Assembly;
PanelMainContent.Children.Add(_userControls[button.Tag.ToString()]);
}
然后在我看来,学生应该将文本块设置为学生ID:
public partial class ViewStudent : UserControl
{
string myProperty = (string)App.Current.Properties["groupboxHeader"];
public ViewStudent()
{
}
protected override void OnInitialized(EventArgs e)
{
textBlock1.Text = myProperty; // error occurs on this line
但它并没有给出一个错误,指出对象引用未设置为对象的实例。
我认为可能存在以下问题:
App.Current.Properties["groupboxHeader"] = groupbox.Header;
我不认为它在做我需要做的事情,那就是当我单击动态添加的按钮时将 app.propertys 设置为当前的组框标题。
编辑:
<UserControl x:Class="WpfApplication4.AppPages.ViewStudent"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock Height="23" HorizontalAlignment="Left" Margin="180,56,0,0" Name="textBlock1" VerticalAlignment="Top" />
</Grid>
</UserControl>