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

    请高人指点。。。。

  • 相关阅读:
    disruptor架构三 使用场景 使用WorkHandler和BatchEventProcessor辅助创建消费者
    disruptor架构二
    disruptor架构一
    线程池基础二
    线程池基础一
    多线程集成设计模式--MasterWorker模式讲解(一)
    多线程集成设计模式--future模式
    线程基础9-quene讲解
    线程基础8-quene讲解
    hdu3613 Best Reward
  • 原文地址:https://www.cnblogs.com/KimhillZhang/p/2417388.html
Copyright © 2011-2022 走看看