DataBind到WPF中的文本框

Ste*_*eve 2 c# data-binding wpf textbox

我对WPF中的数据绑定完全不熟悉,我正在尝试将对象属性绑定到文本框.我的目标是

public class TestObj
{
    private m_Limit;

    public string Limit
    {
       get 
        {
         return m_Limit;
        }
        set
        {
          m_Limit = value;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

现在,显然我没有把源设置在任何地方,我很困惑.我得到了这个与listview一起工作,但现在我很难过.谢谢你的帮助.

Mar*_*ris 7

您需要设置DataContext.在后面的代码中:

textBox3.DataContext = instanceOfTestObj;
Run Code Online (Sandbox Code Playgroud)

或使用对象数据提供程序

  <Window.Resources>
    <src:TestObj x:Key="theData" Limit="Wibble" />
  </Window.Resources>

  <TextBox Text="{Binding Source={StaticResource theData}..../>
Run Code Online (Sandbox Code Playgroud)

有一个很好的介绍更深入的数据绑定到这里.