后台代码中的数据绑定 [英] Data binding in code-behind

查看:84
本文介绍了后台代码中的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在类实例和两个WPF文本框之间设置绑定。不过,文本框不会更改其状态,我无法弄清自己在做什么。

I want to setup a binding between a class instance and two WPF textboxes. Still, the textboxes don't change their status and I can't figure out what I'm doing wrong.

XAML

<DockPanel>
   <TextBlock Style="{StaticResource caption}">Testing System</TextBlock>
   <TextBlock DockPanel.Dock="Left" x:Name="txt1" Text="DC"/>
   <TextBlock DockPanel.Dock="Right" x:Name="txt2"  Text="PD"/>
   <Button Height="20" Width="100" Click="clickBinding">Bind</Button>
   <Button Height="20" Width="100" Click="clickChangeBinding">Change Status</Button>
</DockPanel>

MainWindow.xaml.cs

MainWindow.xaml.cs

private ADSbinding myADS = new ADSbinding();
private void clickBinding(object sender, RoutedEventArgs e)
{
   Binding b1, b2;

   b1 = new Binding();
   b2 = new Binding();
   b1.Source = myADS.DeviceConfigured;
   b2.Source = myADS.ProcessingData;


   b1.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
   b2.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

   BindingOperations.SetBinding(txt1, TextBlock.TextProperty, b1);
   BindingOperations.SetBinding(txt2, TextBlock.TextProperty, b2); 
   }

   private void clickChangeBinding(object sender, RoutedEventArgs e)
   {
      myADS.changedata();
   }

类别:

public class ADSbinding : INotifyPropertyChanged
{
   public event PropertyChangedEventHandler PropertyChanged;

   private string deviceConfigured = "false";
   private string processingData = "false";

   public ADSbinding()
   {
      ProcessingData = "true";
   }

   // Get-Set methods
   public string DeviceConfigured
   {
      get { return deviceConfigured; }
      set 
      { 
         deviceConfigured = value;
         Changed("DeviceConfigured");
      }
   }
   public string ProcessingData
   {
      get { return processingData; }
      set
      {
         processingData = value;
         Changed("ProcessingData");
      }
   }

   private void Changed(string propertyName)
   {
      if (PropertyChanged != null)
         PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
   }

   public void changedata()
   {
      DeviceConfigured = "change";
      ProcessingData = "change";
    }
}

按 clickBinding时,状态会更改,而 clickChangeBinding保留下来,通过再次单击 clickBinding进行更改。这是非常直接的尝试,我不知道问题出在哪里。有人吗?

When pressing "clickBinding" the status changes, when "clickChangeBinding" it remains, by clicking "clickBinding" again it changes. Its a very straight forward attempt and I can't figure out where the problem is. Anyone?

推荐答案

创建绑定时,您正在设置 Source 对象的属性,而不是对象本身。您应该在 Binding 构造函数中指定属性名称,然后将源设置为对象:

When you create the bindings, you are setting Source to the properties of your object, not the object itself. You should specify the property name in the Binding constructor, and then set the source to your object:

    b1 = new Binding("DeviceConfigured");
    b2 = new Binding("ProcessingData");
    b1.Source = myADS;
    b2.Source = myADS;

这篇关于后台代码中的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆