TextBox不会显示绑定的静态通用列表项的内容

时间:2011-12-06 03:46:02

标签: c# wpf xaml data-binding textbox

在WPF中,我在MainWindow.xaml.cs的定义中有一个静态的“Customer”对象。此客户具有可公开访问的字符串Name属性。在表达式混合4中,单击要与客户名称绑定的TextBox的“Text”属性旁边的“高级选项”框。然后我点击“数据绑定...” - > “元素属性” - > “场景元素”下的“窗口” - >单击“ActiveCustomer”旁边的展开箭头 - >然后点击“名称” - > “好”。绑定是一个只读的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; }

1 个答案:

答案 0 :(得分:2)

您无法像实例属性那样绑定到静态属性。然后,属性ActiveCustomer在名为window的元素上不存在,它存在于类MainWindow中。您应该能够使用Sourcex:Static

来修复绑定
{Binding Name, Source={x:Static local:MainWindow.ActiveCustomer}}

请注意,x:Static具有非常特定的语法,不允许任意路径等。

即使绑定有效,但这是有问题的,因为你不能implement INPC用于静态属性,所以如果你将一个新对象分配给ActiveCustomer,则不会有更新。

相关问题