我有一段代码,它应该将文本块添加到一个已经用xaml创建的堆栈面板中。然后我想用一个扩展器包围这个堆栈面板。但是,当我运行它的时候,扩展器根本就没有出现。
要添加到文本块的代码(例外列表):
public void Update(AggregateException ae)
{
StackPanel exceptionsSP = this.ExceptionSP;
Expander exceptionExpander = new Expander();
exceptionExpander.Name = "Exceptions";
exceptionExpander.Header = ae.Message;//The outer exception message
exceptionExpander.IsExpanded = false;
exceptionExpander.ExpandDirection = ExpandDirection.Down;
List<TextBlock> exceptionTexts = new List<TextBlock>();
foreach (Exception exception in ae.InnerExceptions)
{
TextBlock textBlock = new TextBlock();
textBlock.Text = exception.Message;
exceptionsSP.Children.Add(textBlock);
}
exceptionExpander.Content = exceptionsSP;
}
已创建XAML:
<Grid.RowDefinitions>
<RowDefinition Height="300"/>
<RowDefinition x:Name="OKButton" Height="111"/>
</Grid.RowDefinitions>
<StackPanel x:Name="ExceptionSP" Grid.Row="0">
</StackPanel>
<Button Grid.Row="1" IsCancel="True" VerticalAlignment="Center" Width="50" Height="30"/>
产生的xaml(无扩充项):
1条答案
按热度按时间vybvopom1#
您在代码中创建扩展器,然后将扩展器的内容设置为在XAML中创建的StackPanel。这没有任何意义... Expander在函数完成时消失。扩展器需要在XAML中创建,或者您需要XAML中的某种ContentPresenter来保存Expander。
无需编辑XAML,您可以:创建一个新的内部StackPanel,向其中添加项目,将其设置为扩展器的内容,最后执行
this.ExceptionSP.Content = exceptionExpander
。如果您不想更改XAML,则代码如下所示