【问题标题】:Where is the IsEmpty reference/member in this XAML code?此 XAML 代码中的 IsEmpty 引用/成员在哪里?
【发布时间】:2014-09-06 11:34:39
【问题描述】:

我无法理解 IsEmpty 在这段代码 (Path=Text.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 参考吗? (任何关于它的参考链接都很棒)。

【问题讨论】:

    标签: c# .net wpf xaml


    【解决方案1】:

    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 是一个 IEnumerable(包含字符),但不知道作为幕后视图创建​​的 CollectionView,这个视图确实有 IsEmpty 属性。
    • 这个链接是一个很好的关于wpf data-binding/debugging 的教程,虽然它与这个问题无关。所以我在这里添加它给需要的人。
    • IsEmpty 属性可通过ICollectionView 访问字符串s 通过CollectionViewSource.GetDefaultView(s)
    • 所以我在想同样的事情后发现了这个问题。玩了一会儿后,我看到 ReSharper 提出要“限定”访问器,并做了这个:Path=Text.(componentModel:ICollectionView.IsEmpty)
    • TIL 关于 PresentationTraceSources.TraceLevel=High -- 很高兴知道!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 2020-07-01
    • 2022-01-15
    • 2023-03-19
    • 2014-12-15
    • 2017-06-17
    • 1970-01-01
    相关资源
    最近更新 更多