0

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

我的xml:

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

我的视图模型:

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); 数据源.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");
        }
    }
}
4

2 回答 2

1

显示您的保存方法代码。您也必须将消息添加到您的列表中。

void SaveMessage()
{ 

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

}
于 2012-05-10T06:28:16.273 回答
0

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

于 2012-05-10T06:09:58.560 回答