WPF和C#:类和接口问题

时间:2014-03-26 22:24:21

标签: c# wpf

我正在开展银行账户计划。除了主窗口类之外,我还在使用基类,接口和两个派生类。我正在制作一个WPF应用程序,到目前为止,我能够在该应用程序中使用类对象的ArrayList填充ListBox就好了。但是,当我去修改ArrayList中的任何对象时,我很难正确地重新填充ListBox。任何帮助将不胜感激!

这是我的MainWindow代码:

 public partial class MainWindow : Window
 {
    ArrayList bankAccountList = new ArrayList();

    BankAccount savingsAccount = new SavingsAccount("New", "Account", "newaccount");
    BankAccount checkingAccount = new CheckingAccount("New", "Account", "newaccount");
    IAccount iAccount;
    string typeOfAccount = "";

    public MainWindow()
    {
        InitializeComponent();
    }

    //When the user pushes the "Add A Saving Account" button, a new savings account is added to the ArrayList and displayed in the app.
    private void btnAddAccount_Click(object sender, RoutedEventArgs e)
    {
        iAccount = (IAccount)savingsAccount;

        savingsAccount.Deposit(0.00m);

        bankAccountList.Add(savingsAccount);
        lbxExistingAccounts.Items.Add(iAccount.AccountInformation());
        typeOfAccount = "savings";
    }

    //When the user pushes the "Add A Checking Account" button, a new checking account is added to the ArrayList and displayed in the app.
    private void btnAddCheckingAccount_Click(object sender, RoutedEventArgs e)
    {
        iAccount = (IAccount)checkingAccount;

        checkingAccount.Deposit(0.00m);

        bankAccountList.Add(checkingAccount);
        lbxExistingAccounts.Items.Add(iAccount.AccountInformation());
        typeOfAccount = "checking";
    }

    //When the user pushes the "Delete Account" button, the account is removed, and this change is shown in the app.
    private void btnDeleteAccount_Click(object sender, RoutedEventArgs e)
    {
        lbxExistingAccounts.Items.RemoveAt(lbxExistingAccounts.Items.IndexOf(lbxExistingAccounts.SelectedItem));
    }

    //The user can push the "Submit Changes" button to submit his or her changes to the number and name of the account.
    private void btnSubmitChanges_Click(object sender, RoutedEventArgs e)
    {
        try
        {

            for (int index = 0; index < bankAccountList.Count; index++)
            {
                if (index == lbxExistingAccounts.SelectedIndex)
                {
                    if (typeOfAccount == "savings")
                    {
                        savingsAccount.AccountNumber = tbxAccountNumber.Text;
                        savingsAccount.AccountOwnerFirstName = tbxFirstName.Text;
                        savingsAccount.AccountOwnerLastName = tbxLastName.Text;
                    }

                    else if (typeOfAccount == "checking")
                    {
                        checkingAccount.AccountNumber = tbxAccountNumber.Text;
                        checkingAccount.AccountOwnerFirstName = tbxFirstName.Text;
                        checkingAccount.AccountOwnerLastName = tbxLastName.Text;
                    }
                }
            }
            lbxExistingAccounts.Items.Clear();
            foreach (object accountObject in bankAccountList)
            {
                lbxExistingAccounts.Items.Add(accountObject);
            }
        }
        catch (FormatException)
        {
            MessageBox.Show("You may enter changes as letters, numbers, or both.");
        }

    }

这是我的界面代码:

interface IAccount
{
    void SetAccountBalance(decimal accountBalance);
    string AccountInformation();
}

这是我的基类代码:

    abstract class BankAccount
{
    public string AccountNumber { get; set; }
    public string AccountOwnerFirstName { get; set; }
    public string AccountOwnerLastName { get; set; }
    public decimal AccountBalance { get; set; }
    public decimal AnnualInteresetRate { get; set; }
    public string TypeOfAccount { get; set; }

    public BankAccount(string accountOwnerFirstName, string accountOwnerLastName, string accountNumber)
    {
        AccountOwnerFirstName = accountOwnerFirstName;
        AccountOwnerLastName = accountOwnerLastName;
        AccountNumber = accountNumber;
    }

    public abstract void Deposit(decimal amount);

    public abstract void Withdraw(decimal amount);

    public decimal CalculateInterest()
    {
        return (this.AccountBalance * this.AnnualInteresetRate) / 100;
    }
} 

这是我的派生类之一。我两个都差不多。

    class SavingsAccount : BankAccount, IAccount
{
    public SavingsAccount(string accountOwnerFirstName, string accountOwnerLastName, string accountNumber)
        : base(accountOwnerFirstName, accountOwnerLastName, accountNumber)
    {
        AnnualInteresetRate = 0.95m;
    }

    public override void Deposit(decimal amount)
    {
        AccountBalance = AccountBalance + amount;
    }

    public override void Withdraw(decimal amount)
    {
        AccountBalance = AccountBalance - amount;
    }

    public void SetAccountBalance(decimal accountBalance)
    {
        AccountBalance = accountBalance;
    }

    public string AccountInformation()
    {
        return "Savings Account \n  " + AccountOwnerFirstName + " " + AccountOwnerLastName + ", Account#: " + AccountNumber + ", Balance: $" + AccountBalance;
    }
}

2 个答案:

答案 0 :(得分:7)

看起来你刚刚开始,这很好,因为你想要重做一些事情。

  1. 您不需要将派生对象转换为其基本类型。这种类型的铸造被称为&#34;向上铸造&#34;并自动工作,无需任何铸造。

  2. 您发布的代码非常重要&#34; WinForms-ish&#34;这在WPF中不是一个好方法。首先让您的帐户列表成为ObservableCollection并将ListBox的ItemsSource绑定到它。

  3. 该属性看起来像:

    public ObservableCollection<BankAccount> Accounts {get; set;}
    

    实际上应该省略INotifyPropertyChanged以简化和绑定:

    <ListBox "ItemsSource={Binding Accounts}"/>
    

    当然,这应该放在视图模型类中,但您可以通过设置来启动代码隐藏:

    DataContext = this;
    

    一旦有效,所有文本框都应绑定到数据对象或视图本身的属性。

    这是一个关于将MVVM与WPF(MSDN)一起使用的精彩教程。相信我,使用这种设计模式将使您使用WPF的工作变得更加容易,更具可扩展性,并且您会发现WPF基本上是为使用它而设计的。使用WinForms方法只会让你感到痛苦。

    如果我能进一步协助或澄清任何事情,请告诉我!

答案 1 :(得分:1)

我建议您使用ObservableCollection进行收藏,这样对项目所做的任何更改都会在视觉上反映出来,而无需任何额外的工作。它不需要任何重大改变。您要做的就是将ArrayList切换为ObservableCollection

http://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx

我还强烈建议为您的对象实施INotiftyPropertyChanged interfrace,以便在更改帐户信息时通知相应的订阅者。

http://msdn.microsoft.com/en-us/library/vstudio/system.componentmodel.inotifypropertychanged

我在WPF应用程序中广泛使用这两种方法。