zoukankan      html  css  js  c++  java
  • WPF绑定基础

    1、

    XAML:<TextBox Name="textbox1"></TextBox>

    cs:

        public class Customer
        {
            public string Name { get;set;}
        }

               Customer customer = new Customer()
                {
                    Name = "Zhangjinshan"
                };
                Binding bind = new Binding();
                bind.Source = customer;

                bind.Path = new PropertyPath("Name");
                textbox1.SetBinding(TextBox.TextProperty, bind);

    2、

    XAML: <TextBox Name="textbox1" Text="{Binding Path=Name}"></TextBox>

    cs:

               Customer customer = new Customer()
                {
                    Name = "Zhangjinshan"
                };
                textbox1.DataContext = customer;

     

    3、

    XAML:

       <Window.Resources>
            <my:Customer x:Key="mycustomer" Name="ZhangJinshan"/>
        </Window.Resources>
        <StackPanel>
            <StackPanel.DataContext>
                <StaticResource ResourceKey="mycustomer"/>
            </StackPanel.DataContext>
            <TextBox Name="textbox1" Text="{Binding Path=Name}"/>
        </StackPanel>

     

    4、

    <Window.Resources>
            <my:Customer x:Key="mycustomer" Name="Zhangjinshan"/>
        </Window.Resources>
        <StackPanel>
            <TextBox Name="textbox1" Text="{Binding Path=Name, Source={StaticResource ResourceKey=mycustomer}}"/>
        </StackPanel>

     

     

    有一个问题,就是很多地方都写可以这样绑定:

    <TextBox DataContext="{Binding ElementName=customer}" Text="{Binding Path=Name}"/>

     可我怎么试都不行,可能是我理解有误:代码如下:

    XAML:

    <TextBox DataContext="{Binding ElementName=customer}" Text="{Binding Path=Name}"/>

    cs:

    Customer customer = new Customer()
                {
                    Name = "Zhangjinshan"
                };

    请高人指点。。。。

  • 相关阅读:
    并查集
    强联通分量,缩点
    最短路径
    最小生成树
    拓扑排序
    图的遍历
    图论基础知识
    数据库四种隔离级别
    MySQL 索引 乐观锁 悲观锁
    MYSQL+正则
  • 原文地址:https://www.cnblogs.com/KimhillZhang/p/2417388.html
Copyright © 2011-2022 走看看