views:

76

answers:

2

I'm making a small personal finance program that uses "envelopes" as the budgeting method. I'm mostly doing this as a means to learn WPF & MVVM. I've run into a problem where I can't databind to the custom properties I created in my custom ObservableCollection, seen here:

public class ObservableEnvelopeCollection : ObservableCollection<Envelope>
{
    public decimal Total
    {
        get
        {
            decimal total = 0;
            foreach (Envelope env in this)
            {
                total += env.Balance;
            }
            return total;
        }
    }

    public decimal SavingsTotal
    {
        get
        {
            blah blah. . .
        }
    }
}

I'm able to databind my datagrid to the collection with no trouble, however below my datagrid I have labels that I need to display the total of balances for the envelopes in the collection. I'm able to databind to the Count property of the collection, which is a property of the parent ObservableCollection class, but I can't databind to Total or SavingsTotal from my custom class.

The label is just blank when the program is run, and if I use the VS2010 designer and go to the little databinding wizard for the label's Content, it has 'Total' underlined and shows the tooltip: "Path item 'Total' could not be resolved." Again, if I change Path to EnvColl.Count it works.

<Label Content="{Binding Path=EnvColl.Total}"/>

One more note, in my ViewModel I can access the Total and SavingsTotal properties in my collection with no problem, it's just in XAML/databinding that it doesn't work.

A: 

Bah! I hate when things like this happen... I went to add BindsDirectlyToSource=true per Kent and added it to the wrong place, causing a build error. I then removed it and rebuilt, and the databinding suddenly started working. . . :\ I had rebuilt the solution several times with no luck, but causing a compile error then removing it made things start working. Wonder if there's something buggy in 2010's compiler...?

Greg S.
A: 

vs2010

(as always some new commers might need to know about this issue.)

it happened to me when I ran the app on debug mode many times and suddently I have to kill it/stop debugger, then I notice that it does not know when it has to recompile code.

I notice that if I close and reopen the app/solution that helps a lot. if you are running in debugging mode definitely you have to do this every so often.

ramnz