简体   繁体   中英

PropertyPaths and Bindings in Windows Store apps

I'm trying to understand how one of the example games from the windows store samples works. Specifically this one

http://code.msdn.microsoft.com/windowsapps/Reversi-XAMLC-sample-board-816140fa

I understand most of whats going on (I think) but I really have no clue whats going on here:

boardSpace.SetBinding(BoardSpace.SpaceStateProperty,
new Binding { Path = new PropertyPath(String.Format("[{0},{1}]", row, column)) });

I don't understand what the PropertyPath is binding to exactly. It seems to be forming some 2D array so it's binding the SpaceStateProperty to this PropertyPath from the game model view but how does [0,1] or [2, 2] get translated to some specific instance or path?

The next lines make more sense: boardSpace.SetBinding(BoardSpace.CommandProperty, new Binding { Path = new PropertyPath("MoveCommand") });

These are binding the BoardSpacebutton CommandProperty to the MoveCommand Delegate which is exposed in the GameViewModel

Now I found one function thats exposed like this

public BoardSpaceState this[String index]

Would the property path be bound to the this function because it takes a string and the PropertyPath was just a string [x,y]? How does it know?

I feel like I'm missing a subtle part about the way PropertyPath works but reading the doc didn't make much more sense.

I appreciate any help

Yes, this binding is to the "this" property, also known as the indexer, which provides a way to index directly into the defining class as if it were a collection. Thus, the indexer is a sort of default property used whenever there is an index specified, but no specific property name.

The relevant topic in the documentation is Indexers (C# Programming Guide) , which includes the following code snippet (abbreviated here):

class SampleCollection<T>
{
    // Define the indexer, which will allow client code 
    // to use [] notation on the class instance itself. 
    public T this[int i] { /* ... */ }
}

// Usage:
SampleCollection<string> stringCollection = new SampleCollection<string>();
stringCollection[0] = "Hello, World";

This isn't a binding example, but the concept is the same in a binding. A property path of "[ index ]" is a binding to a specific indexed value of the indexer property of the current data context (a GameViewModel object in the example).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM