如果我想让WPF绑定到int
,我需要指定什么绑定路径?我可以这样做吗?WPF绑定到值类型
TextBox textBox = new TextBox();
textBox.DataContext = myValueType;
textBox.SetBinding(TextBox.TextProperty, new Binding("what do I put here?");
如果我想让WPF绑定到int
,我需要指定什么绑定路径?我可以这样做吗?WPF绑定到值类型
TextBox textBox = new TextBox();
textBox.DataContext = myValueType;
textBox.SetBinding(TextBox.TextProperty, new Binding("what do I put here?");
如果我清楚地了解你,你可以写这样的事情INT myValueType
属性绑定到TextBox Text
。
textBox.SetBinding(TextBox.TextProperty, new Binding("DataContext")
{ RelativeSource= new RelativeSource(RelativeSourceMode.Self) });
是的,你可以用:
TextBox textBox = new TextBox();
textBox.DataContext = this;
textBox.SetBinding(TextBox.TextProperty, new Binding("myValueType"));
这里DataContext
变量是包含myValueType
属性的对象。如果该财产在同一班级申报,您可以使用this
。
如果你不想设定TextBox
的DataContext
,你可以使用:
textBox.SetBinding(TextBox.TextProperty, new Binding("myValueType") { Source = TheObjectThatContainsMyValueProperty });
如果不强制使用后面的代码则喜欢XAML代码。用xaml编写和理解这些东西是eaiser。