Sel*_*lva 5 c# wpf nullreferenceexception collectionviewsource
1.有什么区别之间CollectionViewSource,ICollectionView,ListCollectionView的IList的和BindingListCollectionView何时何地使用所有这些东西,我知道主要使用CollectionViewSource的,但即时通讯与何时使用这些不明确的,因为在XAML我使用CollectionViewSource的分组,排序等.当我想在codebehind中使用ListCollectionView时.我对这些事情感到困惑,请任何人解释一下激励差异以及何时使用.谢谢.
小智 2
基本上,CollectionView 是在 WPF 中以最简单的方式构建主细节视图的一种方法。\n首先,我想向您介绍后备机制。
\n<Window.Resources>\n <x:Array x:Key="planets" Type="{x:Type local:Planets}">\n <local:Planets Name="Earth" Diameter="12,756 km" Mass="5.97 10^24kg" Density="5514 kg/m\xc2\xb3"/>\n <local:Planets Name="Mars" Diameter="6792 km" Mass="0.642 10^24kg" Density="3933 kg/m\xc2\xb3"/>\n <local:Planets Name="Jupiter" Diameter="142,984 km" Mass="1898 10^24kg" Density="1326 kg/m\xc2\xb3"/>\n </x:Array>\n </Window.Resources>\n <DockPanel DataContext="{StaticResource planets}">\n <ListBox ItemsSource="{Binding}"\n IsSynchronizedWithCurrentItem="True"\n DisplayMemberPath="Name" Width="125"/>\n <StackPanel>\n <TextBlock Text="{Binding Name}"/>\n <TextBlock Text="{Binding Diameter}"/>\n <TextBlock Text="{Binding Mass}"/>\n <TextBlock Text="{Binding Density}"/>\n </StackPanel>\n </DockPanel>\nRun Code Online (Sandbox Code Playgroud)\npublic class Planets\n {\n public string Name { get; set; }\n public string Diameter { get; set; }\n public string Mass { get; set; }\n public string Density { get; set; }\n }\nRun Code Online (Sandbox Code Playgroud)\n我们有一个类型为 的简单数组Planets。\n还设置了DisplayMemberPath-PropertyName,因此行星名称显示在 中ListBox。除此之外ListBox,各个行星的详细信息也显示在 中TextBlock。Vourla,我们有工作主详细视图,隐式使用了CollectionViewvia 后备机制
由于设置了该属性IsSynchronizedWithCurrentItem="True",WPF 会Default-CollectioView在后台生成一个 并将所选对象保存在ListBox中CurrentItem-Property。现在的情况是Binding第一个在数组上TextBlock搜索 the ;\xe2\x80\x99 在那里找不到任何东西,但后备机制将其转发到 的。\n也可以直接引用的。为此,请在前面做一个简单的操作PropertyPlanets[]CurrentItem-PropertyDefault-CollectionViewCurrentItem-PropertyDefault-CollectionView(/)PropertyBinding一个简单的操作。
<TextBlock Text="{Binding /Name}"/>\nRun Code Online (Sandbox Code Playgroud)\n后备机制仅在设置Path-Property后才起作用Binding。
CollectionViewSource派生自CollectionView并且是提供某些Properties诸如Source或 的代理View。
还有其他三个类派生自CollectionView,它们是ItemCollection、ListCollectionView和BindingListCollectionView。类的接口,即它们前面的类型,是为了避免与类的直接耦合。遵循的原则称为依赖注入(I)
这三个类 ItemCollection、ListCollectionView和BindingListCollectionView是为不同的目的而设计的。ItemCollection因此提供了一个IEnumerable、ListCollectionView一个IList和BindingListCollectionView一个IBindingListObject。因此,为不同的目的提供了不同的类型。
总而言之,CollectionView是一个功能强大的WPF工具;您可以排序、分组等并显示详细信息。
我希望这对您有进一步的帮助。问候迈克尔
\n