WPF Binding Syntax

When to use the "/" character in the path? I thought that "/" only uses when we work with XML, but today we see an example:

class ViewModel { CollectionView Data {get;set;} } class BusinessObkect { string Name {get;set;} } 

The DataContext property of the window is set as an instance of the ViewModel class, the Data property of the ViewModel instance is nitrated using a collection of BusinessObject objects.

if the Text property of the TextBox instance {Binding Path=Data/Name} everything works fine, but if Text = {Binding Path=Data.Name} - a binding error.

When should I use "/" instead of ".". Bound Path?

+4
source share
2 answers

Why not ask for documentation :

Property subvariants can be specified using syntax similar to that used in C #. For example, the Path = ShoppingCart.Order clause sets the binding to the Order subtask of the object or the ShoppingCart property.

When the source is a collection view , the current item can be indicated by a slash (/). For example, the Path = / clause binds to the current element in the view. When the source is a collection, this syntax defines the current collection view element by default.

(link for collection added for convenience)

It is about as concise and complete as it turns out. Use of notation . with a collection in the property of one of its elements does not even make sense. e.g. Collection.Date as opposed to Collection/Date (unless the collection itself has a Date property for any reason).

+11
source

You need to use it when Data is a collection. / takes the current element of the Data collection and returns the Name property on it.

+4
source

All Articles