2014-01-14 43 views
2

我很难让我的WPF正确使用Databinding。在XAML我有以下几点:DataBinding到TextBox不起作用

.... 
<TextBox Name="txt_FirstName" Text="{Binding Path=currentApplication.FirstName, UpdateSourceTrigger=PropertyChanged}" /> 
.... 

我有以下CS代码:

namespace WPF1 
{ 
    public partial class MainWindow : Window 
    { 
    personalApp currentApplication = new personalApp(); 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    } 
} 

引用以下两类:

class personalApp : INotifyPropertyChanged 
{ 
    private Person person = new Person(); 

    public string FirstName 
    { 
    get { return person.FirstName; } 
    set 
    { 
     person.FirstName = value; 
     this.OnPropertyChanged("FirstName"); 
    } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    void OnPropertyChanged(string propName) 
    { 
    if (this.PropertyChanged != null) 
     this.PropertyChanged(
     this, new PropertyChangedEventArgs(propName)); 
    } 

} 

class Person 
{ 
    private string firstName = ""; 

    get { return firstName; } 
    set { FirstName = value; } 
} 

我暂停在代码并逐步检查,但是当我更新应用程序中的txt_FirstName时,它似乎永远不会设置firstName Object。

我哪里错了?

回答

4

您需要更新您的XAML绑定,并使用TextBox设置窗口的DataContext

namespace WPF1 
{ 
    public partial class MainWindow : Window 
    { 
    personalApp currentApplication = new personalApp(); 

    public MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = currentApplication; 
    } 
    } 
} 

更新XAML:

<TextBox Name="txt_FirstName" Text="{Binding FirstName, UpdateSourceTrigger=PropertyChanged}" /> 
-1

你所说的 “更新txt_FirstName应用程序” 是什么意思?
如果直接设置文本框的值,那么你应该尝试设置的currentApplication而不是文本框的值值

0
public MainWindow() 
{ 
    DataContext = this; 
    InitializeComponent(); 
} 

,或者如果你不想数据上下文分配给自己(窗口),因为你可能有其他的datacontext进入窗口,您可以在XAML补充一点: 给你的窗口名称:

<Window .... x:Name="this"... 

然后

<TextBox Name="txt_FirstName" Text="{Binding ElementName=this, 
            Path=currentApplication.FirstName/> 
1

我已更正了代码。

对于文本框:

<TextBox Name="txt_FirstName" Height="30" Background="Beige" 
      Text="{Binding Path=FirstName, UpdateSourceTrigger=PropertyChanged}" /> 

C#代码

namespace Wpf1 
{ 
/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = new personalApp(); 
    } 
} 

internal class personalApp : INotifyPropertyChanged 
{ 
    private Person person = new Person(); 

    public string FirstName 
    { 
     get { return person.FirstName; } 
     set 
     { 
      person.FirstName = value; 
      this.OnPropertyChanged("FirstName"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void OnPropertyChanged(string propName) 
    { 
     if (this.PropertyChanged != null) 
      this.PropertyChanged(
      this, new PropertyChangedEventArgs(propName)); 
    } 
} 

internal class Person 
{ 
    public string FirstName { get; set; } 
} 
} 
相关问题