2013-07-14 84 views
1

此代碼工作在4.0,但拋出ApplicationException: Binding.StaticSource cannot be set while using Binding.Source.在4.5:.NET 4.5斷裂靜態綁定

public class Test 
{ 
    public static string Prop { get; set; } 
} 

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    var binding = new Binding 
    { 
     Source = typeof(Test), 
     Path = new PropertyPath(typeof(Test).GetProperty("Prop")), 
     Mode = BindingMode.OneWayToSource, 
     UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged 
    }; 
    BindingOperations.SetBinding(textBox1, TextBox.TextProperty, binding); 
} 

有沒有解決辦法?目標是以編程方式綁定到靜態屬性(OneWayToSource),而不實例化Test

回答

3

你不需要Source財產static bindings。這甚至不會在.Net 4.0中運行。刪除Source財產,它會 -

var binding = new Binding 
{ 
    Path = new PropertyPath(typeof(Test).GetProperty("Prop")), 
    Mode = BindingMode.OneWayToSource, 
    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged 
}; 
BindingOperations.SetBinding(textBox1, TextBox.TextProperty, binding); 
+1

謝謝,它的工作原理。我的代碼在4.0中運行。 – Poma