zoukankan      html  css  js  c++  java
  • WPF之Binding

    Binding即绑定,听上去像一个音译词,很有趣,我们生活中常用的类似还有一个词,叫want to,我们叫妄图,只不过略加了感情色彩,扯远了,回到正题说绑定。

    感觉这个东西在WPF中的处于核心位置,他就像一座桥梁,把我们要处理的UI、逻辑数据联系到了一起,也正因为他,使得我们的开发理念从UI驱动转向了数据驱动,使我们能够更专注于业务逻辑。

    既然说Binding像桥梁,我们就来说说桥的两端,以及他们是如何联系的。

    我们把这座桥的两端分别叫做Source(源)和Target(目标),当然哪端是源,哪段是目标都是可以相互转化的,还可能是双向的。

    1.Source的范围非常广,凡是可以封装数据,并且通过属性的方式暴露出来的都可以作为源

    2.Target就有一定的限制,因为绑定只能绑定到目标的依赖属性上,所以说只有DependcyObject对象才可以是绑定的对象

    常用的指定方式

    1.普通的CPR对象

    2.CLR集合 包括数组 List 一般是把集合数据指定给Item Control类的Item Source充当数据源

    3.ADO.Net对象 包括DataTable DataView

    4.DataProvider 这其中包括XmlDataProvider和ObjectDataProvider,DataProvider是实现了INotifyPropertyChanged接口的类,所以可以做到及时通知

    5.DependcyObject 依赖对象不但可以做为绑定目标,也可以做源

    6.LINQ

    7.指定源的时候还可以通过ElementName用于XAML代码中不能访问UI对象的时候 RelativeSource用于相对自身的时候

    指定方法

    通过BindingOperations.SetBinding(DependencyObject Target,DependencyProperty db,Binding binding)方法,第一个参数表示绑定到哪个对象上,第二个参数表示绑定到对象的哪个属性上,第三个参数表示应用哪个绑定

    Binding对象有这么几个重要属性

    Source绑定源 可以为空 为空的时候表示取当前对象的DataContext属性(可以继承自父容器)

    Path 源的哪个属性 为空的时候表示源本身就是数值

    Mode 是BindingMode类型的枚举 控制数据流向

    UpdateSourceTrigger UpdateSourceTrigger类型的枚举 控制更新的时机

    当绑定对象为FrameworkElement类型的时候,绑定方法可以是SetBinding(DependcyProperty dp,Binding binding)因为做了封装,BindingOperations.SetBinding方法的第一个参数设为this

    下面是两个DataProvider作为源的例子

    XMLProvider写在窗体Resurce中
    <Window x:Class="DataProviderBindingPractise.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="205" Width="300">
        <Window.Resources>
            <XmlDataProvider x:Key="xmlPro" Source="XMLFile1.xml" XPath="Data/Student"></XmlDataProvider>
        </Window.Resources>
        <StackPanel>
            <ListView x:Name="lvStu" Height="130" Margin="5" ItemsSource="{Binding Source={StaticResource xmlPro}}">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Id" Width="80" DisplayMemberBinding="{Binding XPath=@Id}"/>
                        <GridViewColumn Header="Name" Width="80" DisplayMemberBinding="{Binding XPath=Name}"/>
                        <GridViewColumn Header="Age" Width="80" DisplayMemberBinding="{Binding XPath=Age}"/>
                    </GridView>
                </ListView.View>
            </ListView>
            <!--<Button x:Name="btnLoad" Height="25" Margin="5,0" Click="btnload_Click"/>-->
        </StackPanel>
    </Window>

    也可以写在CodeBehinde中

    View Code
    void InitList()
            {
                XmlDataProvider xdp = new XmlDataProvider();
                xdp.Source = new Uri(@"http://www.cnblogs.com/XMLFile1.xml", UriKind.Relative);
                xdp.XPath = @"Data/Student";
    
                this.lvStu.DataContext = xdp;
                this.lvStu.SetBinding(ListView.ItemsSourceProperty, new Binding());
            }

    ObjectDataProvider

    1.Calculator具体类
    class Calculator
        {
            public string Add(string arg1, string arg2)
            {
                double x = 0;
                double y = 0;
                if (double.TryParse(arg1, out x) && double.TryParse(arg2, out y))
                {
                    return (x + y).ToString();
                }
                else
                {
                    return "Input Error";
                }
            }
        }
    绑定对象
    //ObjectDataProvider作为数据源
            private void SetCalculatorBinding()
            {
                ObjectDataProvider odb = new ObjectDataProvider();
                odb.ObjectInstance = new Calculator();
                odb.MethodName = "Add";
                odb.MethodParameters.Add("0");
                odb.MethodParameters.Add("0");
    
                Binding binding1 = new Binding("MethodParameters[0]") 
                { 
                    Source = odb, 
                    BindsDirectlyToSource = true, 
                    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged 
                };
    
                Binding binding2 = new Binding("MethodParameters[1]")
                {
                    Source = odb,
                    BindsDirectlyToSource = true,
                    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                };
    
                Binding binding3 = new Binding(".") { Source=odb };
    
                txt1.SetBinding(TextBox.TextProperty, binding1);
                txt2.SetBinding(TextBox.TextProperty, binding2);
                txt3.SetBinding(TextBox.TextProperty, binding3);
    
            }

     ps.今天问了下项目组长现在项目的开发模式,毕竟初来乍到,对开发方式不了解的话,适应起来很慢,就显得非常不好了。问的结果让我松了一口气,因为项目现在还没有使用MVVM的开发模式,还是事件委托的方式,这就跟ASP.Net差不多了,只是前台界面的方式不一样。我的目标是尽快掌握各种方法,然后再选一个方向深入研究,在这个百家争鸣的时代,不多了解一些,难免显得眼界不够开广。

  • 相关阅读:
    Max Sum Plus Plus HDU
    Monkey and Banana HDU
    Ignatius and the Princess IV HDU
    Extended Traffic LightOJ
    Tram POJ
    Common Subsequence HDU
    最大连续子序列 HDU
    Max Sum HDU
    畅通工程再续
    River Hopscotch POJ
  • 原文地址:https://www.cnblogs.com/goldren/p/2814357.html
Copyright © 2011-2022 走看看