2015-11-11 83 views
0

我正在对WPF项目进行一些更改以减少弃用。 我试图做的一件事是将我的Textbox.Text值绑定到一个简单的,如下所示。我用的是立即窗口中显示名称的值WPF - 将文本框文本绑定到类属性

<TextBox x:Name="txtNCM" 
     Grid.Column="1" 
     Margin="5" 
     MaxLength="8" 
     Text="{Binding Path=Name}" 
</TextBox> 
public partial class wCad_NCM : UserControl, INotifyPropertyChanged 
{ 
    private string name; 
    public event PropertyChangedEventHandler PropertyChanged; 

    public string Name 
    { 
     get { return name; } 
     set 
     { 
      name = value; 
      OnPropertyChanged("Name"); 
     } 
    } 

    protected void OnPropertyChanged(string name) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(name)); 
     } 
    } 

    public wCad_NCM() 
    { 
     InitializeComponent(); 

    } 
} 

每次,它显示为。我真的很新的这一点,所以我不得不寻找类似的情况相适应,但我不知道如何使这项工作:(

回答

0

您需要设置DataContext,给Name值。

此外

public wCad_NCM() 
{ 
    InitializeComponent(); 
    DataContext = this; // Sets the DataContext 
    Name = "Test"; 
} 

这应该使其工作,但通常是不好的做法,更多细节请http://blog.scottlogic.com/2012/02/06/a-simple-pattern-for-creating-re-useable-usercontrols-in-wpf-silverlight.html

,我试图运行此就跑:

要做到这一点,改变你的构造,包括这个。变成一个名称隐藏问题。尝试使用Name以外的变量名称,因为FrameworkElement已包含它。

+0

你是最好的,它的工作。谢谢 ;) –