zoukankan      html  css  js  c++  java
  • WPF Databinding examples

    1. 最简单的形式
      <TextBlock Text="{Binding ElementName=lbColor, Path=SelectedItem.Content}" />

      <TextBlock x:Name="tbSelectedColor"
          Text="{Binding ElementName=lbColor, Path=SelectedItem.Content, Mode=OneWay}"
          Background="{Binding ElementName=lbColor, Path=SelectedItem.Content, Mode=OneWay}"/>

      <TextBox x:Name="txtSelectedColor" 
          Text="{Binding ElementName=lbColor, Path=SelectedItem.Content, Mode=TwoWay}"
          Background="{Binding ElementName=lbColor, Path=SelectedItem.Content, Mode=OneWay}"/>
    2. Bingding to XML
      <ListBox x:Name="lbColor"
      IsSynchronizedWithCurrentItem="True"
      ItemsSource="{Binding Source={StaticResource MoreColors}, XPath=color/@name}"/>

      <CollectionViewSource x:Key="personView"
          Source="{Binding Source={StaticResource persons}}">
          <CollectionViewSource.SortDescriptions>
              <ComponentModel:SortDescription PropertyName="City" Direction="Ascending" />
              <ComponentModel:SortDescription PropertyName="FullName" Direction="Descending" />
          </CollectionViewSource.SortDescriptions>
      </CollectionViewSource>

      <StackPanel
          DataContext="{Binding Source={StaticResource personView}}" >
          <TextBlock Text="{Binding Path=FullName}"/>
          <TextBlock Text="{Binding Path=Title}"/>
          <TextBlock Text="{Binding Path=City}"/>
      </StackPanel>

    3. Binding C# code
      ---------------------------------------------------------------------------------------------
      public class Person : INotifyPropertyChanged
      {
          string _firstName;
          string _lastName;
    4.     public string FirstName
          {
              get { return _firstName; }
              set
              {
                  _firstName = value;
                  this.OnPropertyChanged("FirstName");
                  this.OnPropertyChanged("FullName");
              }
          }

          public string LastName
          {
              get { return _lastName; }
              set
              {
                  _lastName = value;
                  this.OnPropertyChanged("LastName");
                  this.OnPropertyChanged("FullName");
              }
          }

          public string FullName
          {
              get
              {
                  return String.Format("{0}, {1}",
                      this.LastName, this.FirstName);
              }
          }

          public event PropertyChangedEventHandler PropertyChanged;

          void OnPropertyChanged(string propName)
          {
              if (this.PropertyChanged != null)
                  this.PropertyChanged(
                      this, new PropertyChangedEventArgs(propName));
          }    
      }
      ---------------------------------------------------------------------------------------------
      var person = new Person { FirstName = "Josh", LastName = "Smith" };
      Binding b = new Binding();
      b.Source = person;
      b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
      b.Path = new PropertyPath("FirstName");
      this.firstNameTextBox.SetBinding(TextBox.TextProperty, b);



       

  • 相关阅读:
    第一次个人项目【词频统计】——PSP表格
    第一次个人项目【词频统计】——需求分析,代码规范,设计思路
    第一次个人项目【词频统计】——测试样例分析&性能分析
    PHP5 构造函数
    简单的NT框架
    今天开始学内核
    人生感悟
    (十四)网络层OSPF协议
    (十三)网络层RIP协议报文格式
    (十一)网络层ICMP
  • 原文地址:https://www.cnblogs.com/mrfangzheng/p/1236265.html
Copyright © 2011-2022 走看看