Wpf 从viewmodel更新listview
我使用的是MVVM,有一个视图可以让用户提交一条消息(比如博客),其中列表“不断填充已发布的消息”。当他们单击save时,这会触发视图模型中的save命令来保存消息。我的问题是,我的listview中的gridview没有更新。我想知道是否有人能帮我。我正处在一个循环的阶段。我知道我丢失了一些代码,如果不是很多的话,但是我的脑细胞被炸了 我的xaml:Wpf 从viewmodel更新listview,wpf,Wpf,我使用的是MVVM,有一个视图可以让用户提交一条消息(比如博客),其中列表“不断填充已发布的消息”。当他们单击save时,这会触发视图模型中的save命令来保存消息。我的问题是,我的listview中的gridview没有更新。我想知道是否有人能帮我。我正处在一个循环的阶段。我知道我丢失了一些代码,如果不是很多的话,但是我的脑细胞被炸了 我的xaml: <Grid Name="grdMessage" HorizontalAlignment="Left" Width="816"> &l
<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}" />
observeCollection消息列表代码>
RelayCommand保存代码>
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()
{
将消息添加到消息列表和
OnPropertyChanged(“MessageList”)
在viewmodel的SaveMessage()函数中。显示您的保存方法代码。您还必须将该消息添加到您的列表中
void SaveMessage()
{
this.MessageList.Add(this.Message);
//does excecution to database to save.
}
它是否在代码中的某个地方,比如messageList.add(myNewMessage)?您正在添加到数据库,但是否也更新了messageList?您不需要OnPropertyChanged,messageList是一个ObservableCollection。哦,是的,它不是必需的,因为messageList是ObservableCollection。只需将消息添加到messagelist就足够了。@Phil-如果我不添加OnPropertyChanged,我将不会显示任何内容。无需手动引发On property change事件,因为当您使用属性messagelist进行某些更改时,它将自动引发。我收到一个错误“对象引用未设置为对象的实例”当我在我的代码中添加你所拥有的东西时。我必须再次实例化集合吗?
}
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");
}
}
}
void SaveMessage()
{
this.MessageList.Add(this.Message);
//does excecution to database to save.
}