在这个XAML代码中,IsEmpty的引用/成员在哪里?

15

在这段代码(Path=Text.IsEmpty)中,我无法理解IsEmpty是从哪里来的(关于水印文本框):

<Grid Grid.Row="0" Background="{StaticResource brushWatermarkBackground}" 
                   Style="{StaticResource EntryFieldStyle}" >
    <TextBlock Margin="5,2" Text="Type to search ..." Foreground="Gray"
               Visibility="{Binding ElementName=entry, Path=Text.IsEmpty, 
                          Converter={StaticResource BooleanToVisibilityConverter}}"/>
    <TextBox Name="entry" Background="Transparent"/>
</Grid>

您可以看到,一个字符串没有任何IsEmpty属性。一个DependencyProperty也没有任何IsEmpty成员。我甚至尝试在对象浏览器窗口中搜索IsEmpty,但没有任何相关的结果解释该代码。

您能解释一下这里的IsEmpty参考吗?(任何关于它的参考链接都很好)。

1个回答

18

IsEmpty是从CollectionView.IsEmpty解析出来的。

如何解析的?

我为绑定应用了高级跟踪。

Visibility="{Binding ElementName=entry,
                     PresentationTraceSources.TraceLevel=High, 
                     Path=Text.IsEmpty, 
                     Converter={StaticResource BooleanToVisibilityConverter}}"

这里是结果

System.Windows.Data Warning: 56 : Created BindingExpression (hash=40147308) for Binding (hash=39658150)
System.Windows.Data Warning: 58 :   Path: 'Text.IsEmpty'
System.Windows.Data Warning: 60 : BindingExpression (hash=40147308): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=40147308): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=40147308): Attach to System.Windows.Controls.TextBlock.Visibility (hash=2939094)
System.Windows.Data Warning: 67 : BindingExpression (hash=40147308): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=40147308): Found data context element: <null> (OK)
System.Windows.Data Warning: 74 :     Lookup name entry:  queried TextBlock (hash=2939094)
System.Windows.Data Warning: 65 : BindingExpression (hash=40147308): Resolve source deferred
System.Windows.Data Warning: 67 : BindingExpression (hash=40147308): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=40147308): Found data context element: <null> (OK)
System.Windows.Data Warning: 74 :     Lookup name entry:  queried TextBlock (hash=2939094)
System.Windows.Data Warning: 78 : BindingExpression (hash=40147308): Activate with root item TextBox (hash=46768536)
System.Windows.Data Warning: 108 : BindingExpression (hash=40147308):   At level 0 - for TextBox.Text found accessor DependencyProperty(Text)
System.Windows.Data Warning: 104 : BindingExpression (hash=40147308): Replace item at level 0 with TextBox (hash=46768536), using accessor DependencyProperty(Text)
System.Windows.Data Warning: 101 : BindingExpression (hash=40147308): GetValue at level 0 from TextBox (hash=46768536) using DependencyProperty(Text): ''
System.Windows.Data Warning: 108 : BindingExpression (hash=40147308):   At level 1 - for String.IsEmpty found accessor <null>
System.Windows.Data Warning: 108 : BindingExpression (hash=40147308):   At level 1 - for EnumerableCollectionView.IsEmpty found accessor RuntimePropertyInfo(IsEmpty)
System.Windows.Data Warning: 104 : BindingExpression (hash=40147308): Replace item at level 1 with EnumerableCollectionView (hash=40847598), using accessor RuntimePropertyInfo(IsEmpty)
System.Windows.Data Warning: 101 : BindingExpression (hash=40147308): GetValue at level 1 from EnumerableCollectionView (hash=40847598) using RuntimePropertyInfo(IsEmpty): 'True'
System.Windows.Data Warning: 80 : BindingExpression (hash=40147308): TransferValue - got raw value 'True'
System.Windows.Data Warning: 82 : BindingExpression (hash=40147308): TransferValue - user's converter produced 'Visible'
System.Windows.Data Warning: 89 : BindingExpression (hash=40147308): TransferValue - using final value 'Visible'

以上跟踪信息中有趣的行

System.Windows.Data Warning: 108 : BindingExpression (hash=40147308):   At level 1 - for String.IsEmpty found accessor <null>
System.Windows.Data Warning: 108 : BindingExpression (hash=40147308):   At level 1 - for EnumerableCollectionView.IsEmpty found accessor RuntimePropertyInfo(IsEmpty)

因此您可以看到,实际上类型String没有IsEmpty

for String.IsEmpty found accessor <null>

但是对于这个字符串的视图是一个 EnumerableCollectionView,它确实有 IsEmpty,并且绑定已经解决为相同的内容

for EnumerableCollectionView.IsEmpty found accessor RuntimePropertyInfo(IsEmpty)

我曾经有同样的看法,直到看到了你的问题。我知道每个集合都有一个视图,但我也很惊讶地看到一个基本类型string(实际上是一个字符集合)也通过CollectionView被使用。最后,祝编码愉快 :) - pushpraj
1
这个链接是一个关于WPF数据绑定/调试的好教程,虽然它与这个问题无关。所以我在这里添加了它,供那些需要的人参考。 - chengzi
1
通过 CollectionViewSource.GetDefaultView(s),可以通过 ICollectionView 访问字符串 sIsEmpty 属性。 - Wouter
4
我发现这个问题后也在思考同样的事情。玩弄了一会儿之后,我发现 ReSharper 提供了“限定”访问器的选项,并将其转化为了这样的表达式:Path=Text.(componentModel:ICollectionView.IsEmpty) - Squirrelkiller
1
今天我了解到PresentationTraceSources.TraceLevel=High这个知识点,非常有用!谢谢。 - Michael G
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接