在ObservableCollection被更改后ListView不会更新

在ObservableCollection被更改后ListView不会更新

问题描述:

我有一个ListView,只要我获取数据就会更新。如果我在ViewModel的构造函数中将项目添加到observableCollection中,ListView会得到更新,但如果我在回调后在某种方法中执行此操作,则不会。在ObservableCollection被更改后ListView不会更新

我花了很多时间在*,我知道有很多相关的问题,但我找不到答案。什么是我的问题是,如果它的工作原理,当我在构造函数中添加值,那么为什么它不工作,如果我在某些方法中添加值。

  • 我使用的ObservableCollection
  • 在XAML

这里设置的DataContext

  • 绑定的ObservableCollection到ListView控件是XAML代码

    <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> 
    

    XAML代码隐藏

    public partial class WeatherForcastControl : UserControl 
    { 
        private WeatherForcastViewModel _viewModel; 
        public WeatherForcastControl() 
        { 
         InitializeComponent(); 
         _viewModel = new WeatherForcastViewModel(); 
         this.DataContext = _viewModel; 
        } 
    } 
    

    这里是模型视图代码

    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; 
        } 
    
  • +1

    MessageFromMain做什么? –

    +0

    我使用MVVM Light,当我有可用的预测条件时,MessageFromMain将被调用。在这个函数中,我只是将forcastList分配给我从这个ViewModel的属性中回调。 查看评论代码。 –

    +0

    啊,我现在看到它,你正在更新更新的集合。 –

    的问题是,你的_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; 
    } 
    
    +0

    谢谢老兄,我现在觉得很愚蠢!哈哈还我花了一段时间来接受bcoz我正在以下错误
    私人无效MessageFromMain(GoToForcast消息) { //了很长时间才接受答案bcoz我这样做 // ForcastList =消息答。 ForcastList; //代替这样做 foreach(for message inFormsList中的ForcastInfo f) ForcastList。加入(F); } } ' –

    +0

    我不知道如何在评论中添加代码 –

    好了,你改变了对象列表属性引用,不通过该方法中的字段设置为一个新的集合告诉它的视图。

    要么只使用在通知物业和评论在二传手只是使它不可能改变的参考,这往往是最好的方法:

    private readonly ObservableCollection<ForcastInfo> _forcastList = new ObservableCollection<ForcastInfo>(); 
    public ObservableCollection<ForcastInfo> ForcastList 
    { 
        get { return _forcastList; } 
    } 
    

    现在,你就不会搞砸绑定的该属性总是指向同一个对象。要用此设置替换列表,只需调用Clear()并添加新对象。