我的ViewModel如下所示:
public class MainViewModel : BaseViewModel
{
public List<Paragraph> Paragraphs { get; set; }
. . .
}
public class Paragraph
{
public List<ParagraphElement> Elements;
. . .
}
我的XAML看起来像这样:
<StackPanel Grid.Row="1">
<ItemsControl ItemsSource="{Binding Paragraphs}">
<ItemsControl ItemsSource="{Binding Elements}" ItemTemplate="{StaticResource ParagraphElements}" />
</ItemsControl>
</StackPanel>
我收到以下错误: &#34;的 XamlParseException &#34;
和其他信息: &#39; 将值添加到&#39; System.Windows.Controls.ItemCollection&#39; 提出异常。&#39;
如何在XAML中绑定此nestes结构?
答案 0 :(得分:1)
您必须为外部ItemsControl设置ItemTemplate。抛出异常是因为您为外部ItemsControl设置了ItemsSource并同时向Items集合添加了一个内部ItemsControl
<StackPanel Grid.Row="1">
<ItemsControl ItemsSource="{Binding Paragraphs}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="Green">
<ItemsControl ItemsSource="{Binding Elements}"
ItemTemplate="{StaticResource ParagraphElements}" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>