从viewmodel更新listview

时间:2012-05-10 03:56:54

标签: wpf

我正在使用MVVM,并且有一个视图,允许用户提交消息(如博客),其中列表“不断填充已发布的消息”。当他们单击“保存”时,将触发视图模型中的save命令以保存消息。我的问题是,我的listview里面的gridview没有更新。我想知道是否有人可以帮助我。我正处于一个我只是在圈子里的地步。我知道如果不是很多代码,我会遗漏一些,但我的脑细胞却被炸掉了。

我的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}" />

我的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 Model

    string message;

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

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

2 个答案:

答案 0 :(得分:1)

显示您的保存方法代码。您还必须将邮件添加到列表中。

void SaveMessage()
{ 

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

}

答案 1 :(得分:0)

将消息添加到MessageList和 OnPropertyChanged("MessageList")在viewmodel的SaveMessage()函数中。