gpt4 book ai didi

c# - ObservableCollection 更改后 ListView 未更新

转载 作者:太空狗 更新时间:2023-10-29 23:02:10 25 4
gpt4 key购买 nike

我有一个 ListView,我会在获取数据后立即对其进行更新。如果我在 ViewModel 的构造函数中向 observableCollection 添加项目,ListView 会更新,但如果我在获得回调后以某种方法执行此操作,则不会更新。

我在 Stackoverflow 上花了很多时间,我知道有很多相关问题,但我找不到答案。困扰我的是,如果在构造函数中添加值时它有效,那么为什么在某些方法中添加值时它不起作用。

  • 我正在使用 ObservableCollection
  • 设置数据上下文
  • 在 XAML 中将 ObservableCollection 绑定(bind)到 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;
}
}

这是模型 View 代码

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

关于c# - ObservableCollection 更改后 ListView 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8204863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com