Win
Win

Reputation: 2613

How to display values of a bound datagrid

I have a WPF application and I'm usign the MVVM design approach.

I have an ObservableObject which holds a list of the class Transaction which is below;

public Transaction
{
    public string Ammount;
    public string Direction;
}

The ObservableObject in my ViewModel is below

public ObservableCollection<Transactions> ListOfUserTransactions
        {
            get { return _localTransactionData; }
            set
            {
                _localTransactionData = value;
                RaisePropertyChanged("ListOfUserTransactions");
            }
        }

And my view binds in this way;

<DataGrid ItemsSource="{Binding ListOfUserTransactions}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Ammount" Binding="{Binding Path=Ammount}"  Foreground="Black" IsReadOnly="True" />

                <DataGridTextColumn Header="Direction" Binding="{Binding Path=Direction}" Foreground="Black" IsReadOnly="True" />
            </DataGrid.Columns>
</DataGrid>

This does work in a way as the number of rows produced equal what i was expecting but all the rows are empty, i presume it is the way i am binding the DataGridTextColumn but im unsure as how to do it, hope there is enough detail in the above question.

EDIT

below is how the transaction list is created

ObservableCollection<Transaction> _localTransactionData = new ObservableCollection<Transaction>();


        public TransactionsViewModel(User passedThroughUser)
        {
            _user = passedThroughUser;
            _listOfTransactions = _user.Transactions;

            foreach (var item in _listOfTransactions)
            {
                LocalTransactionsList tran = new LocalTransactionsList();
                tran.Ammount = item.Ammount.ToString(); ;
                tran.Direction = item.Direction;
                _localTransactionData.Add(tran);
            }
        }

Upvotes: 0

Views: 47

Answers (2)

Gayot Fow
Gayot Fow

Reputation: 8792

You can get the behaviour you are after by changing your Transaction class to this...

public class Transaction : INotifyPropertyChanged
{
    private string _amount;
    public string Amount
    {
        [DebuggerStepThrough]
        get { return _amount; }
        [DebuggerStepThrough]
        set
        {
            if (value != _amount)
            {
                _amount = value;
                OnPropertyChanged("Amount");
            }
        }
    }
    private string _direction;
    public string Direction
    {
        [DebuggerStepThrough]
        get { return _direction; }
        [DebuggerStepThrough]
        set
        {
            if (value != _direction)
            {
                _direction = value;
                OnPropertyChanged("Direction");
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string name)
    {
        var handler = System.Threading.Interlocked.CompareExchange(ref PropertyChanged, null, null);
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

This will enable the WPF binding engine to access your properties and bind them correctly.

More about INPC here http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

Upvotes: 2

sexta13
sexta13

Reputation: 1568

Your problem is here:

public Transaction
{
    public string Ammount;
    public string Direction;
}

should be:

public Transaction
{
    public string Ammount {get;set;}
    public string Direction {get;set;}
}

Binding properties must have the setter and getter.

Upvotes: 4

Related Questions