此代码在 4.0 中有效,但ApplicationException: Binding.StaticSource cannot be set while using Binding.Source.
在 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
.