C# 更改ObservableCollection后ListView未更新
我有一个列表视图,我一得到数据就更新它。如果我在ViewModel的构造函数中向observableCollection添加项,则ListView会得到更新,但如果我在收到回调后以某种方法进行了添加,则ListView不会得到更新。 我在Stackoverflow上花了很多时间,我知道有很多相关的问题,但我找不到答案。困扰我的是,如果我在构造函数中添加值时它起作用,那么为什么我在某个方法中添加值时它不起作用C# 更改ObservableCollection后ListView未更新,c#,wpf,listview,observablecollection,C#,Wpf,Listview,Observablecollection,我有一个列表视图,我一得到数据就更新它。如果我在ViewModel的构造函数中向observableCollection添加项,则ListView会得到更新,但如果我在收到回调后以某种方法进行了添加,则ListView不会得到更新。 我在Stackoverflow上花了很多时间,我知道有很多相关的问题,但我找不到答案。困扰我的是,如果我在构造函数中添加值时它起作用,那么为什么我在某个方法中添加值时它不起作用 我正在使用ObservableCollection 设置数据上下文 在XAML中将Ob
- 我正在使用ObservableCollection
- 设置数据上下文
- 在XAML中将ObservableCollection绑定到ListView
<UserControl x:Class="WFP_Illustrated.UserControls.WeatherForcastControl"
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"
d:DesignHeight="300" d:DesignWidth="300"
>
<UserControl.Resources>
<Style TargetType="TextBlock" x:Key="TextStyle">
<Setter Property="FontFamily" Value="Adobe Caslon Pro"/>
<Setter Property="FontSize" Value="18"/>
<Setter Property="MinWidth" Value="60"/>
</Style>
<DataTemplate x:Key="ForcastTemplate">
<Border BorderBrush="Aqua" BorderThickness="1" Padding="5" Margin="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.ColumnSpan="2" Text="{Binding Path= Day}" Style="{StaticResource TextStyle}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Path= Condition}" Style="{StaticResource TextStyle}"/>
<Image Grid.Row="1" Grid.Column="1" />
<TextBlock Grid.Row="2" Grid.Column="0" Text="{Binding Path= Low}" Style="{StaticResource TextStyle}"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path= High}" Style="{StaticResource TextStyle}"/>
</Grid>
</Border>
</DataTemplate>
</UserControl.Resources>
<Grid>
<ListView Name="ForcastList"
ItemTemplate="{StaticResource ForcastTemplate}"
ItemsSource="{Binding ForcastList}"
Background="SlateGray"
>
</ListView>
</Grid>
private ObservableCollection<ForcastInfo> _forcastList;
public ObservableCollection<ForcastInfo> ForcastList
{
get { return _forcastList; }
//set
//{
// _forcastList = value;
// OnPropertyChanged("ForcastList");
//}
}
public WeatherForcastViewModel()
{
//If this is uncommented , I will see values in ListView
//ForcastInfo forcast = new ForcastInfo();
//forcast.Condition = "clear";
//forcast.Day = "Sunday";
//forcast.High = "70";
//forcast.Low = "50";
//_forcastList = new ObservableCollection<ForcastInfo>();
//ForcastList.Add(forcast);
//ForcastList.Add(forcast);
//ForcastList.Add(forcast);
//ForcastList.Add(forcast);
//ForcastList.Add(forcast);
//ForcastList.Add(forcast);
//Callback from MainView ,forcast data is available
Messenger.Default.Register<GoToForcast>
(
this, (action) => MessageFromMain(action)
);
}
private void MessageFromMain(GoToForcast message)
{
//This should work but its not working
ForcastInfo forcast = new ForcastInfo();
forcast.Condition = "clear";
forcast.Day = "Sunday";
forcast.High = "70";
forcast.Low = "50";
_forcastList = new ObservableCollection<ForcastInfo>();
ForcastList.Add(forcast);
ForcastList.Add(forcast);
ForcastList.Add(forcast);
ForcastList.Add(forcast);
ForcastList.Add(forcast);
ForcastList.Add(forcast);
//ForcastList = message.ForcastList;
}
这是ModelView代码
<UserControl x:Class="WFP_Illustrated.UserControls.WeatherForcastControl"
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"
d:DesignHeight="300" d:DesignWidth="300"
>
<UserControl.Resources>
<Style TargetType="TextBlock" x:Key="TextStyle">
<Setter Property="FontFamily" Value="Adobe Caslon Pro"/>
<Setter Property="FontSize" Value="18"/>
<Setter Property="MinWidth" Value="60"/>
</Style>
<DataTemplate x:Key="ForcastTemplate">
<Border BorderBrush="Aqua" BorderThickness="1" Padding="5" Margin="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.ColumnSpan="2" Text="{Binding Path= Day}" Style="{StaticResource TextStyle}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Path= Condition}" Style="{StaticResource TextStyle}"/>
<Image Grid.Row="1" Grid.Column="1" />
<TextBlock Grid.Row="2" Grid.Column="0" Text="{Binding Path= Low}" Style="{StaticResource TextStyle}"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path= High}" Style="{StaticResource TextStyle}"/>
</Grid>
</Border>
</DataTemplate>
</UserControl.Resources>
<Grid>
<ListView Name="ForcastList"
ItemTemplate="{StaticResource ForcastTemplate}"
ItemsSource="{Binding ForcastList}"
Background="SlateGray"
>
</ListView>
</Grid>
private ObservableCollection<ForcastInfo> _forcastList;
public ObservableCollection<ForcastInfo> ForcastList
{
get { return _forcastList; }
//set
//{
// _forcastList = value;
// OnPropertyChanged("ForcastList");
//}
}
public WeatherForcastViewModel()
{
//If this is uncommented , I will see values in ListView
//ForcastInfo forcast = new ForcastInfo();
//forcast.Condition = "clear";
//forcast.Day = "Sunday";
//forcast.High = "70";
//forcast.Low = "50";
//_forcastList = new ObservableCollection<ForcastInfo>();
//ForcastList.Add(forcast);
//ForcastList.Add(forcast);
//ForcastList.Add(forcast);
//ForcastList.Add(forcast);
//ForcastList.Add(forcast);
//ForcastList.Add(forcast);
//Callback from MainView ,forcast data is available
Messenger.Default.Register<GoToForcast>
(
this, (action) => MessageFromMain(action)
);
}
private void MessageFromMain(GoToForcast message)
{
//This should work but its not working
ForcastInfo forcast = new ForcastInfo();
forcast.Condition = "clear";
forcast.Day = "Sunday";
forcast.High = "70";
forcast.Low = "50";
_forcastList = new ObservableCollection<ForcastInfo>();
ForcastList.Add(forcast);
ForcastList.Add(forcast);
ForcastList.Add(forcast);
ForcastList.Add(forcast);
ForcastList.Add(forcast);
ForcastList.Add(forcast);
//ForcastList = message.ForcastList;
}
private observeCollection\u forcastList;
公共可观测集合ForcastList
{
获取{return\u forcastList;}
//设置
//{
//_forcastList=值;
//OnPropertyChanged(“ForcastList”);
//}
}
公共天气ForCastViewModel()
{
//如果这是未注释的,我将在ListView中看到值
//ForcastInfo forcast=新建ForcastInfo();
//forcast.Condition=“clear”;
//forcast.Day=“星期日”;
//预测。高=“70”;
//forcast.Low=“50”;
//_forcastList=新的ObservableCollection();
//预测列表。添加(预测);
//预测列表。添加(预测);
//预测列表。添加(预测);
//预测列表。添加(预测);
//预测列表。添加(预测);
//预测列表。添加(预测);
//从MainView回调,预测数据可用
Messenger.Default.Register
(
此(操作)=>MessageFromMain(操作)
);
}
私有void MessageFromMain(GoToForcast消息)
{
//这应该行得通,但行不通
ForcastInfo forcast=新建ForcastInfo();
forcast.Condition=“clear”;
forcast.Day=“星期日”;
预测。高=“70”;
forcast.Low=“50”;
_forcastList=新的ObservableCollection();
预测列表。添加(预测);
预测列表。添加(预测);
预测列表。添加(预测);
预测列表。添加(预测);
预测列表。添加(预测);
预测列表。添加(预测);
//ForcastList=message.ForcastList;
}
问题在于您正在将_forecastList设置为ObservableCollection的新实例。只要这样做:
public WeatherForcastViewModel()
{
_forcastList = new ObservableCollection<ForcastInfo>();
//Callback from MainView ,forcast data is available
Messenger.Default.Register<GoToForcast>
(
this, (action) => MessageFromMain(action)
);
}
private void MessageFromMain(GoToForcast message)
{
//This should work but its not working
ForcastInfo forcast = new ForcastInfo();
forcast.Condition = "clear";
forcast.Day = "Sunday";
forcast.High = "70";
forcast.Low = "50";
//_forcastList = new ObservableCollection<ForcastInfo>(); <---- this is messing you up
ForcastList.Add(forcast);
ForcastList.Add(forcast);
ForcastList.Add(forcast);
ForcastList.Add(forcast);
ForcastList.Add(forcast);
ForcastList.Add(forcast);
//ForcastList = message.ForcastList;
}
public WeatherForcastViewModel()
{
_forcastList=新的ObservableCollection();
//从MainView回调,预测数据可用
Messenger.Default.Register
(
此(操作)=>MessageFromMain(操作)
);
}
私有void MessageFromMain(GoToForcast消息)
{
//这应该行得通,但行不通
ForcastInfo forcast=新建ForcastInfo();
forcast.Condition=“clear”;
forcast.Day=“星期日”;
预测。高=“70”;
forcast.Low=“50”;
//_forcastList=new ObservableCollection()
要么只在setter中的通知中使用属性和注释,要么不可能更改引用,这通常是最好的方法:
private readonly ObservableCollection<ForcastInfo> _forcastList = new ObservableCollection<ForcastInfo>();
public ObservableCollection<ForcastInfo> ForcastList
{
get { return _forcastList; }
}
private readonly observateCollection\u forcastList=new observateCollection();
公共可观测集合ForcastList
{
获取{return\u forcastList;}
}
现在您不能搞乱绑定,因为属性始终指向同一个对象。要使用此设置替换列表,只需调用Clear()
并添加新对象。MessageFromMain做什么?我正在使用MVVM Light,当我有可用的预测条件时,MessageFromMain将被调用。在这个函数中,我只是将从回调中获得的forcastList分配给这个ViewModel的属性。请参阅注释代码。啊,我现在看到了,您正在替换collec更新中的提示。谢谢老兄,我现在觉得自己太蠢了!!哈哈,我花了一段时间才接受ans bcoz,我犯了以下错误
private void MessageFromMain(GoToForcast message){//我花了很长时间才接受答案bcoz;//我是这样做的//ForcastList=message.ForcastList;//而不是这样做foreach(message.ForcastList中的forcastinfof){ForcastList.Add(f);}`