WPF-8: Binding -3

From "Introduction to WPF" (Liu Tiemeng) reading notes

J) Use LINQ to retrieve the result as the source of the Binding

The query result of LINQ is an IEnumerable<T> type object, and IEnumerable<T> is derived from IEnumerable, so it can be used as the ItemSource of the list control.

Create a class called Student:

public class Student
{
    public int id{get;set;}
    pblic string Name{get;set;}
    public int Age{get;set;}
}

Design the UI to display a Student collection type object when the Button is clicked.

Querying the collection object: To retrieve all students whose names begin with the letter T from a populated List<Student> object:

this.listViewStudents.ItemsSource=from stu in stuList where stu.Name.StartsWith("T") select stu;

If the data is stored in a populated DataTable object, then:

DataTable dt=this.GetDataTable();
this.listViewStudents.ItemsSource=from row in dt.Rows.Cast<DataRow>()
where Convert.ToString(row["Name"]).StartsWith("T")
select new Student()
{
    Id=int.Parse(row["Id"].ToString()),
    Name=row["Name"].ToString(),
    Age=int.Parse(row["Age"].ToString())
};

If the data is stored in an XML file (D:\RawData.xml), then:

XDocument xdoc=XDocument.Load(@"D:\RawData.xml");
this.listViewStudents.ItemsSource=from element in xdoc.Descendants("Student")
where element.Attribute("Name").Value.StartsWith("T")
select new Student()
{
          Id=int.Parse(element.Attribute("Id").Value),
Name=element.Attribute("Name").Value,
Age=int.Parse(element.Attribute("Age").Value)
};

K) Use ObjectDataProvider

Provide the object as a data source to the Binding. BindsDirectlyToSource=true tells the Binding object to only be responsible for writing the data collected from UI elements to its direct Source (ie ObjectDataProvider object) instead of the Calculator object wrapped by the ObjectDataProvider object (calculate the values ​​in the 2 textboxes through the Calculator and write it to the first three TextBoxes).

When the ObjectDataProvider object is used as the Source of Binding, the object itself represents the data, so the Path here uses "." instead of other Data properties.

The methodParameters of ObjectDataProvider is not a dependency property and cannot be used as the target of Binding;

The concept of data-driven UI requires the use of data objects as the Source of Binding as much as possible, and the UI elements as the Target of Biinding.

L) RelativeSource using Binding

When there is a clear data source, the Binding can be associated with it by assigning Source or ElementName. If you can't determine the name of the Source object, but know that it has a relative relationship with the object that is the Binding target in the UI layout, then To use the RelativeSource property of Binding.

The type of the Model property of the RelativeSource class is the RelativeSourceModel enumeration:

public enum RelativeSourceMode
{
    PreviousData,
    TemplateParent,
    Self,
    FindAncestor,
};

There are also 3 static properties: PreviousData, Self, TemplateParent, all of which are RelativeSource types. These three static properties are to create an instance of RelativeSource, set the Mode property of the instance to the corresponding value, and then return the instance.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325659971&siteId=291194637