C# WPF:将自定义用户控件添加到 Stackpanel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22116004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# WPF: Adding custom Usercontrol to Stackpanel
提问by Tobias Raphael Dieckmann
I tried a few things about adding my own created user control element to listbox or stackpanel, but instead of gaining any success, it causes a NullReferenceExceptionand I have no idea why ...
我尝试了一些关于将我自己创建的用户控件元素添加到列表框或堆栈面板的方法,但没有获得任何成功,而是导致NullReferenceException,我不知道为什么......
my user control is looking like that:
我的用户控件看起来像这样:
public partial class ShiftInformationItem : UserControl
{
public ShiftInformationItem()
{
InitializeComponent();
}
}
and the xaml:
和xaml:
<Grid>
<LabelContent="Benutzername:" />
<Label Content="01.03.2014 14:19" />
<TextBox Text="Eintrag ..." />
<Expander Header="Comments (0)">
<Grid Background="#FFE5E5E5"/>
</Expander>
</Grid>
Inside the main window I can add it in a listbox or a stackpanel without any Trouble:
在主窗口内,我可以将它添加到列表框或堆栈面板中,而不会出现任何问题:
<ListBox>
<controls:ShiftInformationItem />
</ListBox>
or:
或者:
<StackPanel Name="ShiftInformationPanel">
<controls:ShiftInformationItem />
</StackPanel>
But when I try to add it with C#:
但是当我尝试用 C# 添加它时:
ShiftInformationList.Items.Add(new ShiftInformationItem());
ShiftInformationPanel.Children.Add(new ShiftInformationItem());
it causes the NullReferenceExceptionand says the object I want to add is null.
它导致NullReferenceException并说我要添加的对象为空。
Does anybody can explain me why?
有没有人可以解释我为什么?
Im very thankful for all well meaned and helpful answers in advance!
我非常感谢所有善意和有用的答案!
UPDATE:
更新:
public partial class HoBusReceptionMain : Window
{
public HoBusReceptionMain()
{
InitializeComponent();
}
private void RibbonWin_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
RibbonTab r = (RibbonTab)e.AddedItems[0];
switch (r.Header.ToString())
{
case "Shift Information":
InitializeShiftInformationTab();
break;
default:
MessageBox.Show(e.AddedItems[0].ToString());
break;
}
}
private void InitializeShiftInformationTab()
{
//here I want to add the new ShiftInformationItem
}
}
UPDATE 2:
更新 2:
Thanks all comments, it Shows up, that my list || Panel is null ... But both is included in the main window (above HoBusReceptionMain)
感谢所有评论,它显示了,我的列表 || Panel is null ...但两者都包含在主窗口中(在HoBusReceptionMain上方)
I use Ribbons in my application, and when a ribbon tab is selected or loaded the RibbonWin_SelectionChangedEvent is fired ... The list or Panel defined below the ribbon definitions
I use Ribbons in my application, and when a ribbon tab is selected or loaded the RibbonWin_SelectionChangedEvent is fired ... The list or Panel defined below the ribbon definitions
回答by Rohit Vats
I suspect you are adding that before InitializeComponent()gets called.
我怀疑你在InitializeComponent()被调用之前添加了它。
Move the code of adding below InitializeComponent()and it will work like it did from XAML. Issue is controls were not initialized before InitializeComponent()gets called and hence resulting in NullReferenceException.
将添加代码移到 InitializeComponent() 下方,它将像在 XAML 中一样工作。问题是控件在InitializeComponent()被调用之前没有初始化,因此导致NullReferenceException.
public MainWindow()
{
InitializeComponent();
ShiftInformationList.Items.Add(new ShiftInformationItem());
ShiftInformationPanel.Children.Add(new ShiftInformationItem());
// ShiftInformationPanel is null here
}

