wpf 代码隐藏中的数据绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13501306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:19:29  来源:igfitidea点击:

Data binding in code-behind

c#wpfdata-binding

提问by Kale

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.

我想设置一个类实例和两个 WPF 文本框之间的绑定。尽管如此,文本框不会改变它们的状态,我无法弄清楚我做错了什么。

XAML

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

主窗口.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();
   }

Class:

班级:

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

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?

当按下“clickBinding”时,状态会改变,当“clickChangeBinding”保持不变时,再次单击“clickBinding”它会改变。这是一个非常直接的尝试,我无法弄清楚问题出在哪里。任何人?

回答by WildCrustacean

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

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

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

回答by Niki

In addition of bde answer you can make also usage of an extension method that allows you to set the binding directly on the FrameworkElement

除了 bde 答案,您还可以使用扩展方法,该方法允许您直接在 FrameworkElement 上设置绑定

public static void SetBinding(this FrameworkElement target, DependencyProperty property, TargetType source, Expression<Func<TargetType, PropertyType>> property_accessor)
{
  var binding = new Binding(source.PropertyName(property_accessor));
  binding.Source = source;
  target.SetBinding(property, binding);
}


public static string PropertyName(this TargetType obj, Expression<Func> property_accessor)
 { 
return ((MemberExpression)property_accessor.Body).Member.Name; 
}

Instead of typing

而不是打字

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

you can use the extension method SetBinding that is definied above

您可以使用上面定义的扩展方法 SetBinding

txt1.SetBinding(TextBlock.TextProperty, myADS, x => x.DeviceConfigured);
txt2.SetBinding(TextBlock.TextProperty, myADS, x => x.ProcessingData);

This way you avoid passing the name of the property as as string.

这样您就可以避免将属性名称作为字符串传递。