In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
How to carry out Wpf data binding, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Foreword:
The basic steps of data binding:
(1) declare a class and its properties first
(2) initialize class assignment
(3) put the control DataContext= object in the C # code
(4) in interface design, the control gives the property to be bound {the property of the Binding binding class}
Principle: listen to the event mechanism, interface changes have events such as TextChanged, so changing the interface can be synchronously modified to objects
If you want ordinary objects to implement data binding, you need to implement the INotifyPropertyChanged interface to listen to ProperChanged. The specific code is shown as follows:
Class Person:INotifyPropertyChanged {private int age; public int Age {get {return age;} set {this.age = value If (PropertyChanged! = null) {PropertyChanged (this, new PropertyChangedEventArgs ("Age");}
BindingMode enumerated value
The name indicates that OneWay updates the target attribute when the source attribute changes, TwoWay updates the target attribute when the source attribute changes, and updates the source attribute OneTime initially sets the target attribute according to the source attribute when the source attribute changes, and subsequent changes will be ignored. OneWayToSource is similar to OneWay in type, but in the opposite direction. Default this type of binding depends on the target attribute
UpdateSourceTrigger
The name indicates the default value of Default, and the Explicit must update the source only when the BindingExpression.UpdateSource is called explicitly. Update the source value when the LostFocus control loses focus when the target value of the PropertyChanged binding changes.
The interface after running the instance is as follows:
MainWindow.xaml
First, explain Task.Delay () and Thread.Sleep () in C#.
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Thread.Sleep () is the synchronous delay and Task.Delay () is the asynchronous delay.
Thread.Sleep () blocks threads, Task.Delay () does not.
Thread.Sleep () cannot be canceled, but Task.Delay () can.
Task.Delay () essentially creates a task that runs at a given time, and Thread.Sleep () sleeps the current thread for a given time.
Decompile Task.Delay (), which is basically a timer wrapped in a task.
The biggest difference between Task.Delay () and Thread.Sleep () is that Task.Delay () is designed to run asynchronously, and it doesn't make sense to use Task.Delay () in synchronous code; using Thread.Sleep () in asynchronous code is a very bad idea. Task.Delay () is usually called using the await keyword.
My understanding: Task.Delay (), async/await and CancellationTokenSource are combined to achieve controllable asynchronous latency.
MainWindow.xaml.cs
Using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Threading.Tasks; using System.Windows; namespace WpfApp1 {/ MainWindow.xaml interaction logic / public partial class MainWindow: Window {public ObservableCollection stuList; public MainWindow () {InitializeComponent (); this.DataContext = new Student () {Name= "111", Id = 1} Task.Run (async () = > / / start asynchronous thread task {await Task.Delay (3000)) / / delay 3 seconds Dispatcher.Invoke ((Action) delegate / / the main interface display in the thread requires a delegate, otherwise the assignment will not be updated in the interface {this.DataContext = new Student () {Name = "222", Id = 2};});}) This.DataContext = new Student () {Name = "333", Id = 3};} private void BtnCtrl1_Click (object sender, RoutedEventArgs e) {Student stu = new Student () {Id = 4, Name = "Jon", Age = 29}; / / instantiate a Student class and assign a value this.DataContext = stu to class members / / pass the instantiated object to DataContext} private void BtnCtrl2_Click (object sender, RoutedEventArgs e) {ObservableCollection stuList = new ObservableCollection () / / list {new Student () {Id=5, Name= "Tim", Age=29}, new Student () {Id=6, Name= "Tom", Age=28},},} This.listBox1.ItemsSource = stuList; this.listBox2.ItemsSource = stuList; this.listBox2.DisplayMemberPath = "Name"; this.DataContext = stuList;}} public class Student: INotifyPropertyChanged / / create a class Student {private string name; public string Name {get {return name that inherits from INotifyPropertyChanged } set {name = value; if (this.PropertyChanged! = null) {PropertyChanged (this, new PropertyChangedEventArgs ("Name")); / / notify Name binding property change notification event} private int id Public int Id {get {return id;} set {id = value; if (this.PropertyChanged! = null) {this.PropertyChanged.Invoke (this, new PropertyChangedEventArgs ("Id")) / / binding property change notification event to Id} private int age; public int Age {get {return age;} set {age = value If (this.PropertyChanged! = null) {this.PropertyChanged.Invoke (this, new PropertyChangedEventArgs ("Age")); / / notify Age binding property change notification event} public int ID {get; internal set;} public event PropertyChangedEventHandler PropertyChanged }} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 258
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.