WPF 之 Binding深入探讨

来源:互联网 发布:热血江湖网络不稳定 编辑:程序博客网 时间:2024/06/02 10:44

一、Data Binding在WPF中的地位

    展示层和逻辑层的沟通就使用Data Binding来实现。可见,Data Binding在WPF中所起的作用就是高速公路的作用。有了这条高速公路,加工好的数据自动送达用户界面并加以显示,被用户修改过的数据也会自动传回业务逻辑层,一旦数据被加工好又会被送往界面。。。。程序的逻辑层就像是一个强有力的引擎一直在运作,用加工好的数据驱动用户界面也文字、图形、动画等形式把数据显示出来------这就是数据驱动UI。



二、XAML连接源或目标


    <StackPanel>        <ScrollBar x:Name="SB" Orientation="Horizontal" Height="30"                    Minimum="1" Maximum="100" LargeChange="1" SmallChange="1"/>                <Label x:Name="labelSBThumb" Height="30" BorderBrush="Blue" BorderThickness="2"                Content="{Binding Value, ElementName=SB}"/>        <!--{Binding 元素属性, ElementName=数据绑定操作源(元素名)}-->    </StackPanel>

    多个控件数据绑定。DataContext

    <StackPanel DataContext="{Binding ElementName=SB}">                <ScrollBar x:Name="SB" Orientation="Horizontal" Height="30"                    Minimum="1" Maximum="100" LargeChange="1" SmallChange="1"/>                <Label x:Name="labelSBThumb" Height="30" BorderBrush="Blue" BorderThickness="2"                Content="{Binding Path=Value}"/>                <Button Content="Click" Height="200"  FontSize="{Binding Path=Value}"/>    </StackPanel>


三、代码数据绑定(IValueConverter 接口 进行数据转换)

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.Shapes;namespace WpfApplication1{    /// <summary>    /// Window4.xaml 的交互逻辑    /// </summary>    public partial class Window4 : Window    {        public Window4()        {            InitializeComponent();            //创建Binding对象            Binding b = new Binding();            //注册转换器,源,路径            b.Converter = new MyDoubleConverter();            b.Source = SB;            b.Path = new PropertyPath("Value");            //调用Label 的 SetBinding() 方法            this.labelSBThumb.SetBinding(Label.ContentProperty,b);        }    }    //System.Windows.Data 命名空间的 IValueConverter 接口 进行数据转换    public class MyDoubleConverter : IValueConverter    {        //值转换成目标类型        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)        {            double v = (double)value;            return (int)v;        }        //将目标类型转换成值        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)        {            return value;        }    }}

四、绑定DataGrid

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

            List<Student> liststudent=new List<Student>();            liststudent.Add(new Student{Name="小张",Age=18});            liststudent.Add(new Student{Name="小张",Age=20});            GridList.ItemsSource = liststudent;

<DataGrid Height="157" x:Name="GridList"/>


0 0
原创粉丝点击