WPF Notes (1.8 Resources and mappings)--hello,wpf!

Source: Internet
Author: User
Tags bind xmlns

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.