简体   繁体   English

更改ObservableCollection后ListView不更新

[英]ListView not updating after ObservableCollection is changed

I have a ListView which i am updating as soon as i get data . 我有一个ListView,我会在获取数据后立即更新。 ListView gets updated if i add items to observableCollection in the ViewModel`s constructor but not if i do it in some method after i get callback . 如果我在ViewModel的构造函数中向observableCollection添加项目,ListView会更新,但如果我在获得回调后在某些方法中执行此操作则不会更新。

I have spend a lot of time on Stackoverflow and i know there are many related questions but i could not find answer . 我花了很多时间在Stackoverflow上,我知道有很多相关的问题,但我找不到答案。 Whats bugging me is that , if it works when i add values in the constructor then why it doesnt work if i add values in the some method . 什么问题让我烦恼的是,如果我在构造函数中添加值时它会起作用,那么如果我在某个方法中添加值,它为什么不起作用。

  • I am using ObservableCollection 我正在使用ObservableCollection
  • Setting DataContext 设置DataContext
  • Binding ObservableCollection to ListView in XAML 将ObservableCollection绑定到XAML中的ListView

here is the XAML code 这是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 CodeBehind XAML CodeBehind

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

Here is ModelView Code 这是ModelView代码

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

The problem is that you're setting the _forecastList to a new instance of an ObservableCollection. 问题是您正在将_forecastList设置为ObservableCollection的新实例。 Just do this: 这样做:

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

Well, you change the object the list property references and do not tell the view about it by setting the field to a new collection in that method. 好吧,您更改了list属性引用的对象,并且不通过将该字段设置为该方法中的新集合来告诉该视图。

Either only use the property and comment in the notifications in the setter or just make it impossible to change the reference, which often is the best method: 要么只使用属性并在setter中的通知中注释, 要么只是无法更改引用,这通常是最好的方法:

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

Now you cannot mess up bindings as the property always points to the same object. 现在你不能搞乱绑定,因为属性总是指向同一个对象。 To replace the list with this setup just call Clear() on it and add the new objects. 要使用此设置替换列表,只需在其上调用Clear()并添加新对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM