WPF Binding syntax
When to use "/" symbol in path? I thought "/" uses only when we work with XML, but today see example:
class ViewModel
{
CollectionView Data {get;set;}
}
class BusinessObkect
{
string Name {get;set;}开发者_Go百科
}
DataContext property of the window is set to an instance of the ViewModel class, Data property of the ViewModel instance is niitialized with a collection of BusinessObject objects.
if Text property of TextBox instance
{Binding Path=Data/Name}
all works normal, but if Text = {Binding Path=Data.Name}
- binding error.
When I must use "/" instead of "." in bindings Path?
Why not ask the documentation:
Subproperties of a property can be specified by a syntax similar to that used in C#. For instance, the clause Path=ShoppingCart.Order sets the binding to the subproperty Order of the object or property ShoppingCart.
When the source is a collection view, the current item can be specified with a slash (/). For example, the clause Path=/ sets the binding to the current item in the view. When the source is a collection, this syntax specifies the current item of the default collection view.
(Collection view link added for convenience)
That's about as concise and complete as it gets. Using .
notation with a collection to a property of one of its items does not even make sense. e.g. Collection.Date
as opposed to Collection/Date
(unless the collection itself actually has a Date
property for some reason).
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.
精彩评论