2014-01-14 39 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; } 
} 
} 
相關問題