Calvin
Calvin

Reputation: 641

update listview from viewmodel

I am using MVVM and have a view which let users submit a message (like a blog) where the list "keeps populating with messages posted". When they click on save, this triggers the save command in the view model to save the message. My issue is, my gridview inside my listview doesnt update. I want to know if anyone can help me out. I'm at a point where I'm just going in circles. I know I'm missing some if not a lot of code but my brain cells are fried.

my xaml:

<Grid Name="grdMessage" HorizontalAlignment="Left" Width="816">
<Grid.RowDefinitions>
    <RowDefinition Height="auto"></RowDefinition>
    <RowDefinition Height="auto"></RowDefinition>
    <RowDefinition Height="auto"></RowDefinition>
    <RowDefinition Height="auto"></RowDefinition>
    <RowDefinition Height="auto"></RowDefinition>
    <RowDefinition Height="auto"></RowDefinition>
    <RowDefinition Height="auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="auto"></ColumnDefinition>
</Grid.ColumnDefinitions>

<TextBlock Grid.Row="0" Grid.Column="0" Text="Messages" HorizontalAlignment="Left" VerticalAlignment="Bottom" />

<ListView Name="lstMessage" Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Bottom" 
             Width="762" Height="auto" Margin="15,0,0,0" ItemsSource="{Binding Path=MessageList}">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="462" Header="Message">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                            <TextBlock Text="{Binding Path=Message, Mode=TwoWay}" TextAlignment="Left" HorizontalAlignment="Left" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="150" Header="Submitter">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Submitter, Mode=TwoWay}" TextAlignment="Left" HorizontalAlignment="Left" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>
<TextBox Name="txtNewMessage" Grid.Row="4" Grid.Column="0" 
        HorizontalAlignment="Left" VerticalAlignment="Top" 
        Width="762" Height="auto" TextWrapping="Wrap" 
        AcceptsReturn="True" HorizontalScrollBarVisibility="Auto" 
        Visibility="Collapsed" Text="{Binding Path=Message, Mode=TwoWay}"/>

<Button Name="btnAddMessage" Grid.Row="6" Grid.Column="0" Content="Add" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,0,0" Command="{Binding Path=Save}" />

my viewmodel:

Message message;

ObservableCollection<Message> messageList;
RelayCommand save;

    public ObservableCollection<Message> MessageList
    {
       get
       {    

if (messageList == null)
                    messageList = new ObservableCollection<Message>();
       }
    }

    public ICommand Save
    {
        get
        {
            return saveCmd ?? (save =   new RelayCommand(parameter => SaveMessage()));
        }
    }

    void SaveMessage()
    {

this.MessageList.Add(this.Message); dataSource.AddMessage(message);

    }


    Message Model

    string message;

[DataMember]
public int Submitter {get; set;}

[DataMember]
public string Message
{
    get{ return(message);}
    set
    {
        if (message != value)
        {
            message = value;
            OnPropertyChanged("Message");
        }
    }
}

Upvotes: 0

Views: 791

Answers (2)

blindmeis
blindmeis

Reputation: 22445

show your Save methode code. you have to add the message to your list too.

void SaveMessage()
{ 

    this.MessageList.Add(this.Message);
    //does excecution to database to save.

}

Upvotes: 1

Naresh Goradara
Naresh Goradara

Reputation: 1896

Add message to MessageList and OnPropertyChanged("MessageList") at SaveMessage() function of viewmodel.

Upvotes: 0

Related Questions