簡體   English   中英

更改ObservableCollection后ListView不更新

[英]ListView not updating after ObservableCollection is changed

我有一個ListView,我會在獲取數據后立即更新。 如果我在ViewModel的構造函數中向observableCollection添加項目,ListView會更新,但如果我在獲得回調后在某些方法中執行此操作則不會更新。

我花了很多時間在Stackoverflow上,我知道有很多相關的問題,但我找不到答案。 什么問題讓我煩惱的是,如果我在構造函數中添加值時它會起作用,那么如果我在某個方法中添加值,它為什么不起作用。

  • 我正在使用ObservableCollection
  • 設置DataContext
  • 將ObservableCollection綁定到XAML中的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 CodeBehind

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

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

問題是您正在將_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;
}

好吧,您更改了list屬性引用的對象,並且不通過將該字段設置為該方法中的新集合來告訴該視圖。

要么只使用屬性並在setter中的通知中注釋, 要么只是無法更改引用,這通常是最好的方法:

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

現在你不能搞亂綁定,因為屬性總是指向同一個對象。 要使用此設置替換列表,只需在其上調用Clear()並添加新對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM