C# 数据绑定到 WPF 中的文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/853259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataBind to a textbox in WPF
提问by Steve
I'm completely new to databinding in WPF, and I'm trying to bind an object property to a textbox. My object is
我对 WPF 中的数据绑定完全陌生,我正在尝试将对象属性绑定到文本框。我的对象是
public class TestObj
{
private m_Limit;
public string Limit
{
get
{
return m_Limit;
}
set
{
m_Limit = value;
}
}
My XAML looks like
我的 XAML 看起来像
<Window x:Class="NECSHarness2.UpdateJobParameters"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tools="clr-namespace:ManagementObjects;assembly=Core"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="Update Job Parameters" Height="320" Width="460">
<Grid>
<TextBox Text ="{Binding Path = Limit, Mode = TwoWay}" Height="20" HorizontalAlignment="Right" Margin="0,48,29,0" Name="textBox3" VerticalAlignment="Top" Width="161" />
</Grid>
Now, obviously I'm not setting the source anywhere, and I'm very confused. I got this to work with a listview, but now I'm stumped. Thanks for any help.
现在,显然我没有在任何地方设置源,而且我很困惑。我让它与列表视图一起工作,但现在我很难过。谢谢你的帮助。
采纳答案by Martin Harris
You need to set the DataContext. Either in the code behind:
您需要设置 DataContext。或者在后面的代码中:
textBox3.DataContext = instanceOfTestObj;
Or using an object data provider
或者使用对象数据提供程序
<Window.Resources>
<src:TestObj x:Key="theData" Limit="Wibble" />
</Window.Resources>
<TextBox Text="{Binding Source={StaticResource theData}..../>
There is a nice introduction to databinding in more depth here.
有一个很好的介绍,更深入数据绑定这里。
回答by arconaut
If you don't specify binding's Source, RelativeSource or ElementName the Binding uses control's DataContext. The DataContext is passed through the visual tree from upper element (e.g. Window) to the lower ones (TextBox in your case).
如果未指定绑定的 Source、RelativeSource 或 ElementName,则 Binding 将使用控件的 DataContext。DataContext 通过可视化树从上层元素(例如窗口)传递到下层元素(在您的情况下为 TextBox)。
So WPF will search for the Limit property in your Window class (because you've bound the window's DataContext to the window itself).
因此,WPF 将在您的 Window 类中搜索 Limit 属性(因为您已将窗口的 DataContext 绑定到窗口本身)。
Also, you may want to read basics about DataBinding in WPF: http://msdn.microsoft.com/en-us/library/ms750612.aspx
此外,您可能想阅读有关 WPF 中数据绑定的基础知识:http: //msdn.microsoft.com/en-us/library/ms750612.aspx
回答by Thomas Levesque
Unless specified otherwise, the source of a binding is always the DataContext of the control. You have to set the DataContext for your form to the TestObj instance
除非另有说明,否则绑定源始终是控件的 DataContext。您必须将表单的 DataContext 设置为 TestObj 实例
回答by Jacob Adams
For TwoWay binding to work your object also has to implement INotifyPropertyChanged
要使 TwoWay 绑定工作,您的对象还必须实现 INotifyPropertyChanged
http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx
http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx