Finally, I can see that it is already 1:39 points.
This section of this book is really bad, a local make me confused, so, ready to follow his ideas, according to my understanding, rewrite, including the source code.
First, the new version of WPF is no longer available:
? Mapping xmlnamespace= "local" clrnamespace= "Databindingdemo", where Databindingdemo is an example of the namespace.
This property is then set in the Window tab
Xmlns:local= "Local"--Change to:
Xmlns:local= "Clr-namespace:databindingdemo", where the namespace Databindingdemo is mapping with the namespace local. Clr-namespace is a fixed grammar, which can be written in this way.
Next, set up resources in <Window.Resources>:
<local:Nicknames x:Key="XX">
<local:Nickname Name="Don" Nick="Naked" />
<local:Nickname Name="Martin" Nick="Gudge" />
<local:Nickname Name="Tim" Nick="Stinky" />
</local:Nicknames>
Here the local equivalent to Databindingdemo, so there are nickname class, we name the object under this category is called XX, we have to do is to add some resources for XX, is nickname object, so have the corresponding three data.
* Note, x:key is used to get the name of the object being created.
Then bind in the DockPanel, refer directly to the XX resource, the object that you just defined and assigned the value:
<DockPanel DataContext="{StaticResource XX}">
In this way, the corresponding background constructor changes to:
public Window1()
{
InitializeComponent();
this.addButton.Click += addButton_Click;
// get names collection from resources
this.names = (Nicknames)this.FindResource("XX");
// no need to make data available for binding here
//dockPanel.DataContext = this.names;
}
As you can see, the initialization and binding button events have not changed, and the following sentence is the key:
this.names = (Nicknames)this.FindResource("XX");
That is to say, find the XX object of foreground axml, and then transfer to names private variable after unpacking. You do not have to bind the data to the DockPanel because the foreground XAML is already implemented.
--The example discussion is complete.
Finally, let's discuss the XAML mapping syntax: Redraw the picture (there is a problem with the original):