2012-04-17 96 views
1

所以我有一些學生記錄從代碼添加的內容後面,我想從一個用戶控件到另一個用戶控件傳遞一個字符串:對象未設置WPF問題與App.Current.Properties

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 

但它不它給出了一個錯誤,指出對象引用不設置到對象的實例..

我認爲有可能是一個問題:我不認爲它做什麼,我需要做的

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

,那就是在app.propertys設置爲當前組框標題,當我點擊dynamiclly添加按鈕。

編輯:

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

你能告訴在拋出異常的確切行? – 2012-04-17 18:52:16

回答

1

前提是所示的代碼完成,您只需忘了打電話給InitializeComponentViewStudent構造。因此textBlock1null

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

永遠不要忘記調用在OnInitialized基類:

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

是的,確實修復了錯誤,現在我只需要找出爲什麼沒有顯示。 Thx Clemens。 – 2012-04-17 21:30:16

+1

@Kirsty不客氣。但是在描述錯誤時你應該儘量變得更加精確。並學習如何使用調試器。這非常有幫助! – Clemens 2012-04-17 21:34:51