wpf前端的数据绑定主要分为元素、资源以及后台数据三种,元素可以简单的理解为前端的空间数据绑定,资源是在resource里找数据,而后台就是跟cs文件之间的数据互相传递。
先说下元素吧,也就是控件元素,因为代码比较简单,就不上效果了,自己可以把下面两行代码复制到xaml文件里运行看,主要就是一个滑动块与文本框互相影响的效果,如果用winfrom来实现,要为两个控件分别写个方法。
<Slider Name="sd" Width="200" />
<TextBox Text="{Binding ElementName=sd,Path=Value}" Height="40" Width="200"/>
接着说资源数据绑定,用到了resource,可以自己设置数据源到xml文件里,然后前端用key名称来获取值
<Window.Resources >
<TextBlock x:Key="txt">hello world</TextBlock>
</Window.Resources>
<!--资源绑定-->
<TextBox Text="{Binding Source={StaticResource txt}, Path=Text}" TextWrapping="Wrap/>
最后,来讲下重点后台数据绑定,也是wpf精华的地方,后台数据绑定可以把数据绑定到窗口,也可以绑定到控件。
1、先展示一下绑定到控件的写法,先在xaml里写入
<TextBlock x:Name="txt2" Text="{Binding Name,FallbackValue=没找到 }" HorizontalAlignment="Left" Margin="52,140,0,0" TextWrapping="Wrap" Width="100" VerticalAlignment="Top" />
然后在cs文件里写入,这样程序运行后,自动就会在文本框里显示李四的字样了。
//student st = new student() { Name = "张三1" };
//txt2.DataContext = st;
txt2.DataContext = new student()
class student
{
public string Name { get ; set ; } ="李四";
}
2、接着展示一下绑定到窗口的写法,先增加一个类,叫MainViewModel.cs,内容如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace WpfApp7
{
class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private int age;
public int Age
{
get {return age; }
set
{
age = value;
NotifyPropertyChanged("Age");
}
}
public int Height { get => height; set => height = value; }
int height;
public MainViewModel()
{
Age = 18;
Height = 171;
}
}
}
在mianwindow窗口里,通过实例化的方法把值获取到,代码如下:
this.DataContext =new MainViewModel();
private void Button_Click(object sender, RoutedEventArgs e)
{
MainViewModel mvd = (MainViewModel)this. DataContext;
mvd.Age++;//Age会增加,因为有 NotifyPropertyChanged("Age");
mvd.Height++;//Height不会增加
}
最后在xaml里展示出来
<TextBox Text="{Binding Age}" VerticalAlignment="Top" Width="120"/>
3、绑定到后台数据,先上xaml部分文章来源:https://www.toymoban.com/news/detail-445273.html
<DataGrid ItemsSource="{Binding StuCln}" HorizontalAlignment="Left" Height="143" Margin="365,28,0,0" VerticalAlignment="Top" Width="400">
<DataGrid.Columns >
<DataGridTextColumn Header="序号" Binding="{Binding Id}"></DataGridTextColumn>
<DataGridTextColumn Header="姓名" Binding="{Binding Name}"></DataGridTextColumn>
<DataGridTextColumn Header="年龄" Binding="{Binding Age}"></DataGridTextColumn>
</DataGrid.Columns>
接着是代码部分,这里有个mvvm的思想,就是model类和viewmodel类要区分开,本demo是合在一起写的,可以自行分解开看文章来源地址https://www.toymoban.com/news/detail-445273.html
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp9
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
Student stu;
public MainWindow()
{
InitializeComponent();
stu = new Student() { Id = 1, Name = "张三", Age = 18 }; //初始化数据
stu.StuCln = new ObservableCollection<Student>();
stu.StuCln.Add(stu);
this.DataContext = stu;
}
}
public class Student : INotifyPropertyChanged
{
//旧的写法,标准但是繁琐
private int id;
public int Id
{
get { return id; }
set
{
id = value;
NotifyPropertyChanged("Id");
}
}
// 简化后错误写法,无法生效
private string name;
public string Name
{
get => name;
set => setValue(ref name, value,Name);//这种写法值不会变
}
//简化后正确写法
private int age;
public int Age
{
get => age;
set => setValue(ref age, value,nameof(Age));
}
private void setValue<T>( ref T propertyName, T value,string fatherPropertyName)
{
propertyName = value;
NotifyPropertyChanged(fatherPropertyName);
}
ObservableCollection<Student> stuCln;
public ObservableCollection<Student> StuCln
{
get => stuCln;
set => setValue(ref stuCln, value, nameof(StuCln));
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
到了这里,关于wpf数据绑定之元素、资源、后台绑定的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!