我试图设置Items控件的样式并使用此XAML:
<DataTemplate x:Key="DatatemplatePlateItemContainer">
<Grid x:Name="PlateItemContainer">
<!-- Does recursive ItemTemplate selection -->
<ItemsControl ItemsSource="{Binding Path=Children}"
ItemTemplateSelector="{StaticResource TemplateselectorPlateItem}">
<ItemsControl.ItemContainerStyle>
<Style>
<Style.Setters>
<Setter Property="Grid.Row" Value="{Binding Path=GridRow}"/>
<Setter Property="Grid.Column" Value="{Binding Path=GridColumn}"/>
</Style.Setters>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<!-- This line will not let the ItemsControl to load -->
<Rectangle Grid.Row="0" Grid.Column="0" Grid.RowSpan="1" Grid.ColumnSpan="4"/>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</DataTemplate>
如果我尝试在其ItemsPanelTemplate.Grid中添加任何UIElement(此示例中的Rectangle) - 从底部添加第7行 - 它将失败。只是评论一下 - 它的确有效。这个简单的XAML有什么问题?它不会提供太多信息 - 仅在输出中,我无法追踪:
A first chance exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
A first chance exception of type 'System.Xaml.XamlObjectWriterException' occurred in System.Xaml.dll
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: Add value to collection of type 'System.Windows.Controls.UIElementCollection' threw an exception.
An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: Add value to collection of type 'System.Windows.Controls.UIElementCollection' threw an exception.