2016-02-09 79 views
0

如果我想让WPF绑定到int,我需要指定什么绑定路径?我可以这样做吗?WPF绑定到值类型

TextBox textBox = new TextBox(); 
textBox.DataContext = myValueType; 
textBox.SetBinding(TextBox.TextProperty, new Binding("what do I put here?"); 

回答

0

如果我清楚地了解你,你可以写这样的事情INT myValueType属性绑定到TextBox Text

textBox.SetBinding(TextBox.TextProperty, new Binding("DataContext") 
    { RelativeSource= new RelativeSource(RelativeSourceMode.Self) }); 
0

是的,你可以用:

TextBox textBox = new TextBox(); 
textBox.DataContext = this; 
textBox.SetBinding(TextBox.TextProperty, new Binding("myValueType")); 

这里DataContext变量是包含myValueType属性的对象。如果该财产在同一班级申报,您可以使用this

如果你不想设定TextBoxDataContext,你可以使用:

textBox.SetBinding(TextBox.TextProperty, new Binding("myValueType") { Source = TheObjectThatContainsMyValueProperty }); 

如果不强制使用后面的代码则喜欢XAML代码。用xaml编写和理解这些东西是eaiser。