lundi 11 mai 2015

Binding to an array of structs in WPF

In my application I store an array of structs containing how much bills/coins i need to pay to a client and how many I effectively pay.

public struct MoneyType
{ //contains more, but left out for readability
   public int Requested { get; set; }  // How many are requested for a payment
   public int Paid { get; set; } //bills paid to the customer
}

I made an array with these which I make accessible to the viewmodel in the following way:

MoneyType[] money;  //correctly initialised in the constructor
public MoneyType[] GetMoney()
{
    return money;
}

In the viewmodel itself i make these accessible in this way:

public MoneyType[] MoneyTypes //Denominations
{
    get
    {
        return _dataService.GetMoney();
    }
} 

Finally, in my XAML, I accessed these as:

<cbtn:BillCounter x:Name="euro200" Value = "{Binding MoneyTypes[2].Paid, Mode=TwoWay }" />

The [2] is used to indicate which type of bills/coins i want to use.

However, now i want to make this work 2 ways, so that my custom control can update the amount of coins/bills paid. For which I would need a setter for my MoneyTypes property. However, i'm not sure i'm going the right way for this. I have a feeling that someway i should not pass the entire array from my viewmodel/model, but a specific part of one of my MoneyTypes (the paid/requested fields, with some kind of index indicating which entry to use), but i'm unsure of how to do this. Any tips on this?

Thanks!!

Aucun commentaire:

Enregistrer un commentaire