您的位置:首页 > 编程语言 > C#

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;

}

}

}

以上只是实现数据绑定的一部分,在实际的使用中还有其他的方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: