wpf 数据绑定

It would be even better if you could just associate the two controls, without having to write any additional code. This is the goal of data binding.
Data binding is the association of two objects, such that one of the objects is always kept up-to-date with the value of the other. With data binding, you can just create an association between the TextBox.Text property and the Slider.Value property, and WPF will take care of the updates automatically as they occur. This association is called a binding.

对于两个控件之间的绑定有两种方式:
1、从XAML里就绑定

<StackPanel>                                                  
      <TextBox Margin="10" Text="{Binding ElementName=sldrSlider, Path=Value}"/> 
      <Slider Name="sldrSlider" TickPlacement="TopLeft"  Margin="10"/>  
</StackPanel>

2、在后台代码中绑定
假设设计界面已经添加以下两个控件:

<StackPanel> 
      <Label   Name="displayText"/> 
      <TextBox Name="sourceInfo"/> 
   </StackPanel> 

在这里插入图片描述后台绑定:

public Window1() 
   { 
      InitializeComponent(); 
 
      Binding myBinding = new Binding();             
      myBinding.Source  = sourceInfo;                 
      myBinding.Path    = new PropertyPath( "Text" ); 
      // Connect the Source and the Target. 
      displayText.SetBinding( Label.ContentProperty, myBinding ); 
   }

在这里插入图片描述
绑定有以下方向:
在这里插入图片描述
删除绑定:
在这里插入图片描述
在这里插入图片描述

<Window x:Class="ClearBindings.Window1" ... 
      Title="Clear Bindings" Height="152" Width="260"> 
      <StackPanel> 
         <Label Name="displayText" Margin="5" FontSize="16" 
            Content="My Text" 
            FontFamily="{Binding ElementName=fontBox, Path=Text}" 
            FontWeight="{Binding ElementName=weightBox, Path=Text}"/> 
 
         <Grid> 
            <Grid.ColumnDefinitions> 
               <ColumnDefinition></ColumnDefinition> 
               <ColumnDefinition></ColumnDefinition> 
            </Grid.ColumnDefinitions> 
            <StackPanel> 
 
               <ComboBox Name="fontBox" SelectedIndex="0" Margin="5,0,5,2"> 
                  <ComboBoxItem>Arial</ComboBoxItem> 
                  <ComboBoxItem>Courier New</ComboBoxItem> 
               </ComboBox> 
 
               <ComboBox Name="weightBox" SelectedIndex="0" Margin="5,0,5,2"> 
                  <ComboBoxItem>Normal</ComboBoxItem> 
                  <ComboBoxItem>Bold</ComboBoxItem> 
               </ComboBox> 
            </StackPanel> 
 
            <StackPanel Grid.Column="1"> 
               <Button Name="ClearFont" Margin="5,0,5,2" 
                       Click="ClearFont_Click">Clear Font</Button> 
               <Button Name="ClearAll" Margin="5,0,5,2"  
                       Click="ClearAll_Click">Clear All</Button> 
               <Button Name="CreateBindings" Margin="5,0,5,2"  
                       Click="CreateBindings_Click">Create Bindings</Button> 
            </StackPanel> 
         </Grid> 
      </StackPanel> 
   </Window> 

后台代码实现删除绑定、添加绑定:

 public partial class Window1 : Window 
   { 
      public Window1() { InitializeComponent(); } 
 
      // Clear the FontFamily binding. 
      private void ClearFont_Click( object sender, RoutedEventArgs e ) 
      { 
         BindingOperations.ClearBinding( displayText, FontFamilyProperty ); 
      } 
 
      // Clear all the bindings. 
      private void ClearAll_Click( object sender, RoutedEventArgs e ) 
      { 
         BindingOperations.ClearAllBindings( displayText ); 
      } 
 
      // Re-create the two bindings. 
      private void CreateBindings_Click( object sender, RoutedEventArgs e ) 
      { 
         // Create the FontFamily binding. 
         Binding fontBinding = new Binding(); 
         fontBinding.Source = fontBox; 
         fontBinding.Path = new PropertyPath( "Text" ); 
         fontBinding.Mode = BindingMode.OneWay; 
         displayText.SetBinding( FontFamilyProperty, fontBinding ); 
 
         // Create the FontWeight binding. 
         Binding weightBinding = new Binding(); 
         weightBinding.Source = weightBox; 
         weightBinding.Path = new PropertyPath( "Text" ); 
         weightBinding.Mode = BindingMode.OneWay; 
         displayText.SetBinding( FontWeightProperty, weightBinding ); 
      } 
   } 

绑定到非控件:
So far, all the source items for which you’ve created bindings have been WPF elements that derive from UIElement. But the source object can be any type of object. In this section, I’ll show how to create a simple class and bind to several of its properties. Remember, however, that although the source object can be of any type, the Path must always point to a public property.
新建Person.cs文件:

class Person 
   { 
      public string FirstName     { get; set; } 
      public int Age              { get; set; } 
      public string FavoriteColor { get; set; } 
 
      public Person( string fName, int age, string color ) 
      { 
         FirstName     = fName; 
         Age           = age; 
         FavoriteColor = color; 
      } 
   } 

设计器中添加三个label:

   <StackPanel Orientation="Horizontal"> 
      <Label Name="lblFName" FontWeight="Bold"/> 
      <Label Name="lblAge"/> 
      <Label Name="lblColor"/> 
   </StackPanel> 

后台代码实现绑定到Person类的三个属性:

public MainWindow()
        {
            InitializeComponent();

            Person p = new Person("Shirley", 34, "Green");                      
            Binding nameBinding = new Binding("FirstName");
            nameBinding.Source = p;
            lblFName.SetBinding(ContentProperty, nameBinding);

            Binding ageBinding = new Binding("Age");
            ageBinding.Source = p;
            lblAge.SetBinding(ContentProperty, ageBinding);

            Binding colorBinding = new Binding("FavoriteColor");
            colorBinding.Source = p;
            lblColor.SetBinding(ContentProperty, colorBinding);
		}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值