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"
                };

    请高人指点。。。。

  • 相关阅读:
    一失足千古恨在 WSL 中使用了 md 创建文件夹 (2020-04-26)
    开源中国 ThinkPHP 领奖
    投资投机脑图(2019-12-12)
    什么? 1XIN = 21BTC
    笔记:投机和投资 F4NNIU
    如何设置单个 Git 仓库的代理从而提高更新速度
    FastAdmin 使用 phpmail 出现 spl_autoload_register 错误
    plsql 引用型变量
    oracle 存储函数
    oracle存储过程(带参数的存储过程)
  • 原文地址:https://www.cnblogs.com/KimhillZhang/p/2417388.html
Copyright © 2011-2022 走看看