在DataTrigger中绑定问题(Binding problems in DataTrigger)

编程入门 行业动态 更新时间:2024-10-27 19:28:52
在DataTrigger中绑定问题(Binding problems in DataTrigger)

在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')

CollectionViewGroupInternalCollectionViewGroup的内部实现, 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

本文发布于:2023-08-06 11:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1448848.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   DataTrigger   problems   Binding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!