Update Listview

Topics: Getting Started, UI Architecture
Apr 18, 2014 at 7:28 PM
I have a simple listview which displays data from a view model. Then I have 4 textboxes where I can enter data and click on Add button that should add the data in the collection in ViewModel. The same collection is used to display data in ListView.

Now when application loads, ListView displays the 4 records. But when I try to add a new item in collection that does not get updated in ListView.

Following is the code for ViewModel
public class AppViewModel : PropertyChangedBase
    {
        private List<LoanInfo> lstLoans;
        private string inputLoanName;
        private string inputLoanNumber;
        private string inputStatus;
        private decimal inputAmount;

        public List<LoanInfo> LoanList 
        {
            get { return lstLoans; }
            set 
            { 
                lstLoans = value;
                NotifyOfPropertyChange(() => LoanList);
            }
        }

        public string InputLoanName { 
            get
            {
                return inputLoanName;
            } 
            set 
            {
                inputLoanName = value;
                NotifyOfPropertyChange(() => InputLoanName);
            } 
        }
        public string InputLoanNumber
        {
            get
            {
                return inputLoanNumber;
            }
            set
            {
                inputLoanNumber = value;
                NotifyOfPropertyChange(() => InputLoanNumber);
            }
        }
        public string InputStatus
        {
            get
            {
                return inputStatus;
            }
            set
            {
                inputStatus = value;
                NotifyOfPropertyChange(() => InputStatus);
            }
        }
        public decimal InputAmount
        {
            get { return inputAmount; }
            set
            {
                inputAmount = value;
                NotifyOfPropertyChange(() => InputAmount);
            }
        }

        public AppViewModel()
        {
            lstLoans = new List<LoanInfo>();
            lstLoans.Add(new LoanInfo() { LoanName = "Loan1", LoanNumber="1001-1", Amount=100000, Status="Pending"});
            lstLoans.Add(new LoanInfo() { LoanName = "Loan2", LoanNumber = "1001-2", Amount = 200000, Status = "Pending" });
            lstLoans.Add(new LoanInfo() { LoanName = "Loan3", LoanNumber = "1001-3", Amount = 300000, Status = "Pending" });
            lstLoans.Add(new LoanInfo() { LoanName = "Loan4", LoanNumber = "1001-4", Amount = 400000, Status = "Pending" });
            lstLoans.Add(new LoanInfo() { LoanName = "Loan5", LoanNumber = "1001-5", Amount = 500000, Status = "Pending" });

        }

        public void AddLoan()
        {
            LoanList.Add(new LoanInfo() { LoanName = InputLoanName, LoanNumber = InputLoanNumber, Amount = InputAmount, Status = InputStatus });
            NotifyOfPropertyChange(() => LoanList);
            
        }
    }
Follwoing is the code of xaml file
<UserControl x:Class="LoanDetails.Views.AppView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d">
    <UserControl.Resources>
        <Style TargetType="TextBox">
            <Setter Property="Background" Value="Beige"></Setter>
            <Setter Property="Margin" Value="2"></Setter>
            <Setter Property="Width" Value="100"></Setter>
            <Setter Property="HorizontalAlignment" Value="Left"></Setter>
            <Setter Property="VerticalAlignment" Value="Center"></Setter>
        </Style>
    </UserControl.Resources>
    <Grid Height="400" Width="800" ShowGridLines="True">
        <ListView Height="Auto" Width="Auto" Name="LoanList" ItemsSource="{Binding LoanList, NotifyOnSourceUpdated=True, Mode=TwoWay}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="LoanName" DisplayMemberBinding="{Binding LoanName}"></GridViewColumn>
                    <GridViewColumn Header="LoanNumber" DisplayMemberBinding="{Binding LoanNumber}"></GridViewColumn>
                    <GridViewColumn Header="Status" DisplayMemberBinding="{Binding Status}"></GridViewColumn>
                    <GridViewColumn Header="Amount" DisplayMemberBinding="{Binding Amount}"></GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
        <Grid Height="100" VerticalAlignment="Bottom" ShowGridLines="True" Margin="0,0,0,40">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="25"></RowDefinition>
                <RowDefinition Height="25"></RowDefinition>
                <RowDefinition Height="25"></RowDefinition>
                <RowDefinition Height="25"></RowDefinition>
            </Grid.RowDefinitions>
            <Label Content="Name" Grid.Column="0" Grid.Row="0"></Label>
            <TextBox Name="InputLoanName"  Grid.Column="1" Grid.Row="0"></TextBox>
            <Label Content="Number" Grid.Column="0" Grid.Row="1"></Label>
            <TextBox Name="InputLoanNumber"  Grid.Column="1" Grid.Row="1"></TextBox>
            <Label Content="Status" Grid.Column="0" Grid.Row="2"></Label>
            <TextBox Name="InputStatus"  Grid.Column="1" Grid.Row="2"></TextBox>
            <Label Content="Amount" Grid.Column="0" Grid.Row="3"></Label>
            <TextBox Name="InputAmount"  Grid.Column="1" Grid.Row="3"></TextBox>
        </Grid>
        <Button Name="AddLoan" Content="Add" Height="30" Width="50" VerticalAlignment="Bottom" HorizontalAlignment="Right"></Button>
    </Grid>
</UserControl>
As I'm new to Caliburn.Micro, can someone plesae help?
Apr 18, 2014 at 8:14 PM
The issue was resolved after changing
List<LoanInfo> LoanList
To
ObservableCollection<LoanInfo> LoanList