2012-11-21 99 views
1

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

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

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”它会改变。它是一个非常直接的尝试,我不知道问题在哪里。任何人?

回答

1

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

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

非常感谢! – RobaL

2

此外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; 
} 

而不是键入

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

您可以使用所definied扩展方法SetBinding以上

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

这种方式可以避免将属性的名称作为字符串传递。

+0

TargetType在哪里? – Rhyous