Where is the IsEmpty reference/member in this XAML code?
Asked Answered
G

1

16

I cannot understand where the IsEmpty comes from in this snippet of code (Path=Text.IsEmpty) (about a watermark TextBox):

<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>

You can see a string does not have any IsEmpty property. A DependencyProperty also does not have any IsEmpty member. I've even tried searching the IsEmpty in the Object Browser window but there was not any relevant result explaining the code.

Could you explain to me the IsEmpty reference here? (Any reference link about it is great).

Glandular answered 6/9, 2014 at 11:34 Comment(0)
A
18

IsEmpty is being resolved from CollectionView.IsEmpty

how?

I applied high tracing for the binding

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

and here is the result

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'

interesting lines from the above trace

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)

so you can see that indeed type String does not have IsEmpty

for String.IsEmpty found accessor <null>

but the view for the string is an EnumerableCollectionView which does have IsEmpty and the binding resolved to the same

for EnumerableCollectionView.IsEmpty found accessor RuntimePropertyInfo(IsEmpty)
Azevedo answered 6/9, 2014 at 12:3 Comment(8)
thanks. I know string is an IEnumerable (contains characters) but not know about the CollectionView created as the view behind the scene, this view does have IsEmpty property.Glandular
I did have the same perception till I saw your question. I knew that every collection has a view, but I am also amused to see that a basic type string (actually a collection of chars) is also being used via a CollectionView. last but not the least happy coding :)Azevedo
This link is a good tutorial airtle about wpf data-binding/debugging , although it has nothing to do with this question. So i add it here for those who need it.Greenlet
This IsEmpty property is accesible via the ICollectionView for a string s via CollectionViewSource.GetDefaultView(s)Gonfanon
So I found this question after wondering the same thing. After playing around for a moment, I saw that ReSharper offered to "Qualify" the accessor, and made this of it: Path=Text.(componentModel:ICollectionView.IsEmpty)Ohm
TIL about PresentationTraceSources.TraceLevel=High -- good to know! thank you.Tarentarentum
This is cool and in fact is works fine. But VS 2022 keeps giving me this error "XLS0432 The property 'IsEmpty' was not found in type 'String'.". I can't leave it this way. Any chance to get rid of this error?Pilau
@KarloX, see if this helps learn.microsoft.com/en-us/visualstudio/ide/…Azevedo

© 2022 - 2024 — McMap. All rights reserved.