2013-07-14 3 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