对象引用未设置App.Current.Properties的wpf问题

时间:2012-04-17 18:45:50

标签: c# wpf xaml object instance

所以我有一些学生记录,从后面的代码中添加内容,我试图将一个字符串从一个用户控件传递给另一个用户控件:

    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()]);
    }

然后在我的视图中学生应该将文本块设置为studentID:

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

但它没有给出一个错误,说Object引用没有设置为对象的实例..

我认为可能存在以下问题:

App.Current.Properties["groupboxHeader"] = groupbox.Header;

我不认为它正在做我需要做的事情,那就是当我点击动态添加按钮时将app.propertys设置为当前的groupbox标题。

编辑:

<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>

1 个答案:

答案 0 :(得分:1)

如果显示的代码已完成,您只需忘记在InitializeComponent构造函数中调用ViewStudent。因此textBlock1null

public ViewStudent() 
{ 
    InitializeComponent(); // add this
} 

永远不要忘记在OnInitialized中调用基类:

protected override void OnInitialized(EventArgs e)
{
    base.OnInitialized(e); // and add this
    ...
}