2

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.

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.

I'm not sure the right way to do this. I have a feeling that somehow, 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)

I'm unsure of how to do this.

3
  • 1
    I'm afraid. Databinding wont work with struct. You should really make it a class if you need it to be two way. Commented May 11, 2015 at 15:12
  • How would I handle this if i change the struct to a class? It's mainly the Set part that I don't get. How do I tell the model that that entry in the array has changed? Or am I already doing it wrong by passing the entire array to the viewmodel from the model, and should i do that differently? Commented May 11, 2015 at 15:32
  • You have to implement INotifyPropertyChanged, so that your changes would be visible on view. Your struct doesn't implement it, so even if you change something from ViewModel it won't be changed in View. Commented May 11, 2015 at 18:00

1 Answer 1

1

May this answer is helps you - https://stackoverflow.com/a/7714924/3207043

Based on this, you need to use reference data types to bindings.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.