Idle talk about the second of WPF (data processing in WPF [3])

Source: Internet
Author: User

I have been busy recently and haven't written WPF for many days. Today, we continue to go back to the previous topic: Data Processing in WPF. As mentioned above, by implementing INotifyPropertyChanged, we can change to make any CLR object support the binding source of WPF. However, INotifyPropertyChanged is usually only applied to a single class attribute. In practical applications, we also encounter another situation: we need to monitor whether a pile of data changes. That is to say, the data source we bind is no longer a separate data object. For example, when the source is a data table, we want to be notified of any changes to the data in the table. (Currently, the support for ADO. NET for WPF binding is not considered .)

WPF provides an ObservableCollection class that implements a data set that exposes INotifyPropertyChanged. In other words, we do not need to implement the INotifyPropertyChanged structure for each individual data. Let's first look at how to implement a simple binding data set.

Namespace NSLYL
{
Public class LYLDataObj
{
Public LYLDataObj (string name, string description)
{
This. name = name;
This. description = description;
}

Public string Name
{
Get {return name ;}
Set {name = value ;}
}

Public string Description
{
Get {return description ;}
Set {description = value ;}
}

Private string name;
Private string description;
}

Public class LYLDataObjCol: ObservableCollection <LYLDataObj>
{
Public LYLDataObjCol ()
{
This. Add (new LYLDataObj ("Microsot", "Operating System "));
This. Add (new LYLDataObj ("Google", "Search "));
}
}
}

The code is very simple. It is basically such a template. Then, we can bind LYLDataObjCol to an Element that requires multiple data items, such as ListBox and ComboBox.

<ListBox ItemsSource = "{StaticResource dataObj}".../>

After binding, as long as my LYLDataObjCol object sends a change, the data of ListBox and ComboBox will also change accordingly.

Now, we know that there are two ways to specify the data source when binding: 1. DataContext. We will briefly introduce it in this Post. 2. directly use the Source attribute of the Binding class. So what are the differences between them? First, the priority of the Source is higher than that of the DataContext. Only the Source does not exist, or the DataContext will be searched when the current Source does not reach the required attribute. There is no real difference between the two, but we recommend that you use Source to help us debug the application. Because it can clearly obtain the Source information. DataContext supports inheritance. You can specify the Source in the parent Element. This also becomes an advantage of DataContext: if multiple elements need to be bound to the same Source, we only need to specify DataContext in one place to use its sub-Element.

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.