2011-12-06 3 views
0

WPFでは、MainWindow.xaml.csの定義に静的な "Customer"オブジェクトがあります。この顧客には、一般にアクセス可能な文字列Nameプロパティの文字列があります。 Expression Blend 4では、顧客名にバインドするTextBoxの「Text」プロパティの横にある「Advanced Options」ボックスをクリックします。次に、 "データバインディング..." - > "要素要素" - > "ウィンドウ要素" - > "ActiveCustomer"の横にある拡大矢印をクリックし、 "名前" - > "OK"をクリックします。バインドは読み取り専用のTextBoxに対して行われるため、一方向バインディングはデフォルトとして使用できます。しかし、私は私のアプリを実行すると、それは顧客の名前を表示しません。助言がありますか?TextBoxはバインドされた静的汎用リスト項目の内容を表示しません

<TextBox x:Name="AIAHNameTextBox" Height="26" Canvas.Left="90" TextWrapping="Wrap" Canvas.Top="8" Width="100" IsReadOnly="True" VerticalContentAlignment="Center" Text="{Binding ActiveCustomer.Name, ElementName=window, Mode=OneWay}" /> 

ActiveCustomerは私のCustomerクラスのインスタンスである:

namespace WPFBankingSystem 
{ 
    public enum CustomerStatus 
    { Open, Closed } 
    public enum TransferType 
    { CheckingToSaving, SavingToChecking } 

    [Serializable] 
    public class Customer 
    { 
     private string address; 
     private Checking chkAcc; 
     private string name; 
     private int pin; 
     private Saving savAcc; 
     private string ssn; 
     private AccountStatus status; 
     private string tel; 

     public string Address 
     { 
      get { return address; } 
      set { address = value; } 
     } 
     public Checking ChkAcc 
     { 
      get { return chkAcc; } 
      set { chkAcc = value; } 
     } 
     public string Name 
     { get { return name; } } 
     public int Pin 
     { 
      get { return pin; } 
      set { pin = value; } 
     } 
     public Saving SavAcc 
     { 
      get { return savAcc; } 
      set { savAcc = value; } 
     } 
     public string Ssn 
     { get { return ssn; } } 
     public AccountStatus Status 
     { 
      get { return status; } 
      set { status = value; } 
     } 
     public string Tel 
     { 
      get { return tel; } 
      set { tel = value; } 
     } 

     public void create(string Name, string Address, string TelephoneNumber, string SSN, int PIN) 
     { 
      this.address = Address; 
      this.name = Name; 
      this.pin = PIN; 
      this.ssn = SSN; 
      this.status = AccountStatus.Open; 
      this.tel = TelephoneNumber; 
     } 
     public void delete() 
     { 
      if (this.chkAcc != null) 
      { chkAcc.close(); } 
      if (this.savAcc != null) 
      { savAcc.close(); } 
     } 
     public bool hasChkAcc() 
     { return (this.chkAcc != null) ? true : false; } 
     public bool hasSavAcc() 
     { return (this.savAcc != null) ? true : false; } 
     public void show() 
     { } 
     public void transfer(double Amount, TransferType Type) 
     { 
      if(this.hasChkAcc() && this.hasSavAcc()) 
      { 
       switch(Type) 
       { 
        case TransferType.CheckingToSaving: 
         this.chkAcc.Balance -= Amount; 
         this.savAcc.Balance += Amount; 
         break; 
        case TransferType.SavingToChecking: 
         this.savAcc.Balance -= Amount; 
         this.chkAcc.Balance += Amount; 
         break; 
       } 
      } 
      else 
      { throw new Exception("You do not have both a checking account and a saving account."); } 
     } 

     public Customer() 
     { } 
     ~Customer() 
     { this.delete(); } 
    } 
} 

インサイドMainWindow.xaml.cs、顧客は単に公共Customerオブジェクトとして定義されます。

public Customer ActiveCustomer 
{ get; set; } 
+0

「ActiveCustomer」の定義を投稿した場合は参考になります。 –

+0

私がActiveCustomerの定義をしたのは、MainWindow上のActiveCustomerというメンバを意味し、そのクラスはインスタンスではありません。 –

答えて

2

あなたがすることはできませんインスタンスプロパティにできるような静的プロパティにバインドします。プロパティActiveCustomerは、windowという名前の要素には存在しません。クラスMainWindowに存在します。

{Binding Name, Source={x:Static local:MainWindow.ActiveCustomer}} 

注意をx:Staticが、それは任意のパスなどを許可していない、非常に具体的な構文を持っていること:あなたはx:Staticと一緒にSourceを使用して結合を修正することができるはずです。

静的プロパティの場合はimplementINPCを指定することはできませんが、バインディングが問題になる場合でも、新しいオブジェクトをActiveCustomerに割り当てると更新されません。

+0

プロパティを非静的にリセットしようとしましたが、それもうまく機能しませんでした。 –

+0

それは公共の財産であり、畑ではありませんか? –

+0

正解でしたが、正解でした。しかし、この定義は、静的プロパティが別々に扱われると言った後です。 –

関連する問題