在Josh Smith的文章中, “使用Model-View-ViewModel设计模式的WPF应用程序”
在AllCustomersView.xaml的代码中:
<DataTrigger Binding="{Binding Path=Name}" Value="True"> <Setter TargetName="txt" Property="Text" Value="Company" /> </DataTrigger>我无法找到属性 - 名称。 我认为这是在CustomerViewModel.cs中。 但事实并非如此。 我怎么知道名字的路径?
In Josh Smith's article,"WPF Apps With The Model-View-ViewModel Design Pattern"
In the code from AllCustomersView.xaml:
<DataTrigger Binding="{Binding Path=Name}" Value="True"> <Setter TargetName="txt" Property="Text" Value="Company" /> </DataTrigger>I couldn't find out where the property--Name is. I thought it was in the CustomerViewModel.cs. But indeed it wasn't. How could I know the path of Name?
最满意答案
它来自CollectionViewSource:
<CollectionViewSource x:Key="CustomerGroups" Source="{Binding Path=AllCustomers}"> <CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName="IsCompany" /> </CollectionViewSource.GroupDescriptions> <CollectionViewSource.SortDescriptions> <scm:SortDescription PropertyName="IsCompany" Direction="Descending" /> <scm:SortDescription PropertyName="DisplayName" Direction="Ascending" /> </CollectionViewSource.SortDescriptions> </CollectionViewSource>当您使用CVS时,它会将您的数据(本例中为CustomerViewModel实例的集合)分解为多个组(在本例中,基于属性IsCompany)。
每个组都放置在一个CollectionViewGroup中 。 然后将这些组的集合绑定到将显示组的控件。 所以Name属性实际上是在CollectionViewGroup上找到的,并且是IsCompany的值(即PropertyGroupDescription)或布尔值的字符串值。
如果您将绑定更改为无效
<DataTrigger Binding="{Binding Path=Derp}" Value="True"> <Setter TargetName="txt" Property="Text" Value="Companies" /> </DataTrigger>您会看到它停止工作,并且在输出窗口中显示以下消息:
System.Windows.Data错误:39:BindingExpression路径错误:'对象''' CollectionViewGroupInternal '(HashCode = 41413147)'找不到'Derp'属性。 BindingExpression:路径= DERP; DataItem =' CollectionViewGroupInternal '(HashCode = 41413147); 目标元素是'ContentPresenter'(Name =''); 目标属性是'NoTarget'(键入'Object')
CollectionViewGroupInternal是CollectionViewGroup的内部实现, CollectionViewSource用于将属性IsCompany上的集合分组
It comes from the CollectionViewSource:
<CollectionViewSource x:Key="CustomerGroups" Source="{Binding Path=AllCustomers}"> <CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName="IsCompany" /> </CollectionViewSource.GroupDescriptions> <CollectionViewSource.SortDescriptions> <scm:SortDescription PropertyName="IsCompany" Direction="Descending" /> <scm:SortDescription PropertyName="DisplayName" Direction="Ascending" /> </CollectionViewSource.SortDescriptions> </CollectionViewSource>When you use a CVS, it takes your data (in this case, a collection of CustomerViewModel instances), and splits them into groups (in this case, based on the property IsCompany).
Each group is placed in a CollectionViewGroup. A collection of these groups is then bound to the control that will be showing the groups. So the Name property is actually found on the CollectionViewGroup, and is the value of IsCompany (that's the PropertyGroupDescription), or the string value of a boolean.
If you change the binding to something invalid
<DataTrigger Binding="{Binding Path=Derp}" Value="True"> <Setter TargetName="txt" Property="Text" Value="Companies" /> </DataTrigger>you'll see it stop working, and the following message show up in your output window:
System.Windows.Data Error: 39 : BindingExpression path error: 'Derp' property not found on 'object' ''CollectionViewGroupInternal' (HashCode=41413147)'. BindingExpression:Path=Derp; DataItem='CollectionViewGroupInternal' (HashCode=41413147); target element is 'ContentPresenter' (Name=''); target property is 'NoTarget' (type 'Object')
CollectionViewGroupInternal is the internal implementation of CollectionViewGroup that the CollectionViewSource uses to group your collection on the property IsCompany
更多推荐
In,CustomerViewModel,电脑培训,计算机培训,IT培训"/> <meta name="descript
发布评论