C# WPF数据绑定
2015-08-27 16:19
357 查看
在使用WPF设计可执行程序的时候,应该尽可能的避免直接操作控件。
这就涉及到数据绑定的问题。数据绑定有什么作用?如何实现?
在实现数据绑定的时候首先需要定义一个类,这个类实现一些属性值。
例如:在界面上有两个textbox控件,这两个控件主要实现显示姓名和年龄的工作。
首先定义一个Person类,这个类中有两个属性Name和Age,
在界面的类中定义一个Person对象P1,对其进行属性值的赋值。然后就是进行绑定。
具体的参见代码实现。
Person类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace WpfApplication3
{
class Person:INotifyPropertyChanged//实现后台数据与界面数据的同步
{
private string name;
private int age;
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
//数据发生改变时触发这个事件,告诉界面数据发生改变
if (PropertyChanged !=null)
{
PropertyChanged(this,new PropertyChangedEventArgs("Name"));
}
}
}
public int Age
{
get
{
return this.age;
}
set
{
this.age = value;
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs("Age"));
}
}
}
private bool gender;
public bool Gender
{
get
{
return gender;
}
set
{
this.gender = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Gender"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
XAML实现:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1">
<Grid> //属性与数据的绑定,一般的属性都可以实现这样的绑定
<TextBox x:Name="txtName" HorizontalAlignment="Left" Height="23" Margin="43,31,0,0" TextWrapping="Wrap" Text="{Binding Name}"
VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="txtAge" HorizontalAlignment="Left" Height="23" Margin="43,69,0,0" TextWrapping="Wrap" Text="{Binding Age}" VerticalAlignment="Top" Width="120"/>
<Button x:Name="btnAge" Content="Age++" HorizontalAlignment="Left" Margin="198,31,0,0" VerticalAlignment="Top" Width="75" Click="btnAge_Click"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="198,69,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
<CheckBox x:Name="cbGender" Content="CheckBox" HorizontalAlignment="Left" Margin="43,133,0,0" VerticalAlignment="Top" IsChecked="{Binding Gender}"/>
</Grid>
</Window>
XAML交互逻辑实现:
using System;
using System.Collections.Generic;
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 WpfApplication3
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
Person p1 = new Person();
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
p1.Name = "杨中科";
p1.Age = 20;
p1.Gender = true;
//这个就是数据上下文的绑定。
txtName.DataContext = p1;
txtAge.DataContext = p1;
cbGender.DataContext = p1;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
MessageBox.Show(txtAge.Text.ToString());
}
private void btnAge_Click(object sender, RoutedEventArgs e)
{
p1.Age++;
p1.Gender = false;
}
}
}
以上只是实现数据绑定的一部分,在实际的使用中还有其他的方法。
这就涉及到数据绑定的问题。数据绑定有什么作用?如何实现?
在实现数据绑定的时候首先需要定义一个类,这个类实现一些属性值。
例如:在界面上有两个textbox控件,这两个控件主要实现显示姓名和年龄的工作。
首先定义一个Person类,这个类中有两个属性Name和Age,
在界面的类中定义一个Person对象P1,对其进行属性值的赋值。然后就是进行绑定。
具体的参见代码实现。
Person类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace WpfApplication3
{
class Person:INotifyPropertyChanged//实现后台数据与界面数据的同步
{
private string name;
private int age;
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
//数据发生改变时触发这个事件,告诉界面数据发生改变
if (PropertyChanged !=null)
{
PropertyChanged(this,new PropertyChangedEventArgs("Name"));
}
}
}
public int Age
{
get
{
return this.age;
}
set
{
this.age = value;
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs("Age"));
}
}
}
private bool gender;
public bool Gender
{
get
{
return gender;
}
set
{
this.gender = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Gender"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
XAML实现:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1">
<Grid> //属性与数据的绑定,一般的属性都可以实现这样的绑定
<TextBox x:Name="txtName" HorizontalAlignment="Left" Height="23" Margin="43,31,0,0" TextWrapping="Wrap" Text="{Binding Name}"
VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="txtAge" HorizontalAlignment="Left" Height="23" Margin="43,69,0,0" TextWrapping="Wrap" Text="{Binding Age}" VerticalAlignment="Top" Width="120"/>
<Button x:Name="btnAge" Content="Age++" HorizontalAlignment="Left" Margin="198,31,0,0" VerticalAlignment="Top" Width="75" Click="btnAge_Click"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="198,69,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
<CheckBox x:Name="cbGender" Content="CheckBox" HorizontalAlignment="Left" Margin="43,133,0,0" VerticalAlignment="Top" IsChecked="{Binding Gender}"/>
</Grid>
</Window>
XAML交互逻辑实现:
using System;
using System.Collections.Generic;
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 WpfApplication3
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
Person p1 = new Person();
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
p1.Name = "杨中科";
p1.Age = 20;
p1.Gender = true;
//这个就是数据上下文的绑定。
txtName.DataContext = p1;
txtAge.DataContext = p1;
cbGender.DataContext = p1;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
MessageBox.Show(txtAge.Text.ToString());
}
private void btnAge_Click(object sender, RoutedEventArgs e)
{
p1.Age++;
p1.Gender = false;
}
}
}
以上只是实现数据绑定的一部分,在实际的使用中还有其他的方法。
相关文章推荐
- C# 窗体程序 DataGridView 输出换行
- C# 控制台程序如何防止启动多个实例
- C#语言特性-运算符重载
- c# 线程启动
- C# 内存垃圾回收
- C# 程序运行时间检测打印
- C# List排序
- BeginInvoke 用法 2 - 异步返回
- Visual C#2010学习笔记九之.dll类库文件的引用
- c#调用GetWindowText函数
- C# 类的属性,方法,事件
- 知识点滴(一) c#副屏设置以及读写ini文件的方法
- 实现Textbox+listBoxControl拼音检索 C#
- C# 实现无边框窗口拖动
- C#中实现文本框的滚动条自动滚到最底端
- C# 工厂单例
- c#中预处理指令来实现条件编译
- C# 单例3种写法
- c#中各种进制之间转换
- 【c#】关于终止以及删除主进程创建的线程以及进程的方法