WPF 复合集合

问题描述 投票:0回答:1

我正在浏览WPF官方文档,如何:实现CompositeCollection,对CollectionContainer有一些疑问。

<ListBox.ItemsSource>
    <CompositeCollection>
         <CollectionContainer Collection="{StaticResource greekGods}" />
         <CollectionContainer Collection="{Binding Source={StaticResource greekHeroes}}" />
         <ListBoxItem Background="red">Other ListBox Item 1</ListBoxItem>
         <ListBoxItem Background="red">Other ListBox Item 2</ListBoxItem>
     </CompositeCollection>
</ListBox.ItemsSource>

为什么 XMLDataProvider 资源

GreekHeroesData
必须使用 Binding?否则会抛出异常。

<CollectionContainer Collection="{Binding Source={StaticResource greekHeroes}}" />

但是CLR资源

GreekGodsData
可能会忽略它?

<CollectionContainer Collection="{StaticResource greekGods}" />

感谢您的帮助!

当我写下以下内容时:

 <CollectionContainer Collection="{StaticResource greekHeroes}" />

它引发了异常:

System.Windows.Markup.XamlParseException
  HResult=0x80131501
  Message=“An exception was thrown while setting the property 'System.Windows.Data.CollectionContainer.Collection'.”, the line number is "49", and the line position is "22".
  Source=PresentationFramework
  StackTrace:
   at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at CompositeCollections.MainWindow.InitializeComponent() in I:\github.com\learning-wpf\DataBinding\CompositeCollections\MainWindow.xaml : line 1

  This exception was initially thrown in this call stack:
    [external code]

Internal exception 1:
ArgumentException: 'System.Windows.Data.XmlDataProvider' is not a valid value for property 'Collection'.

感谢您的帮助!

wpf data-binding
1个回答
0
投票
  1. 为什么 XMLDataProvider 资源 GreekHeroesData 必须使用 Binding?
  • 正如您在文档链接中看到的,在他们的 XAML 中,他们定义了 [XmlDataProvider][1],这意味着您可以在 XAML 文件而不是视图模型中创建数据 -> 因此 GreekHeroesData 必须使用 Binding
  1. CLR 资源 GreekGodsData 可能会忽略它?
  • GreekGodsData他们从另一个资源filfe(clr-namespace:SDKSample)获取,因此需要使用静态资源来获取它。
  1. 当我用以下内容编写时,它引发了异常?
© www.soinside.com 2019 - 2024. All rights reserved.