[英]WPF Binding to a value type
如果我希望WPF綁定到int
我應該指定什么綁定路徑? 我能做到嗎?
TextBox textBox = new TextBox();
textBox.DataContext = myValueType;
textBox.SetBinding(TextBox.TextProperty, new Binding("what do I put here?");
如果我清楚地理解您的話,您可以編寫類似的內容將int myValueType
屬性綁定到myValueType
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編寫和理解這些內容很容易。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.