此问题在此处已有答案:
using of INotifyPropertyChanged(3个答案)
24天前关闭。
我正在使用MVVM与WPF,并仍在解决问题。
我遇到的问题是,我调用了一个返回数据列表的api。我将数据列表添加到一个可观察的集合中。(当窗口打开时调用此api。)当窗口打开时,GridView是空的。只有当我将数据硬编码到我的构造函数中时,它才会显示。
我希望在打开页面时显示来自API的数据。
我做错了什么?我该如何解决这个问题?
任何帮助和指导将不胜感激。
我的视图xaml
<Window x:Class="ErrorLogger_WPF.Views.ExceptionDetailsListView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ErrorLogger_WPF.Views" xmlns:viewmodels="clr-namespace:ErrorLogger_WPF.ViewModels" d:DataContext="{d:DesignInstance Type=viewmodels:ExceptionDetailsListViewModel}"
mc:Ignorable="d"
Title="ExceptionDetailsListView" Height="450" Width="800">
<Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="15"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListView HorizontalAlignment="Left" VerticalAlignment="Top" Name="ExceptionList" ItemsSource="{Binding ExceptionDetails}" Height="Auto" Grid.Row="1">
<ListView.View>
<GridView>
<GridViewColumn Header="Exception ID" DisplayMemberBinding="{Binding Path=Id}" Width="Auto"/>
<GridViewColumn Header="Exception Type" DisplayMemberBinding="{Binding Path=ExceptionType}" Width="Auto"/>
<GridViewColumn Header="Exception Message" DisplayMemberBinding="{Binding Path=ExceptionMessage}" Width="Auto"/>
<GridViewColumn Header="Inner Exception" DisplayMemberBinding="{Binding Path=InnerException}" Width="Auto"/>
<GridViewColumn Header="Exception Source File" DisplayMemberBinding="{Binding Path=ExceptionSourceFile}" Width="Auto"/>
<GridViewColumn Header="Exception Source Line" DisplayMemberBinding="{Binding Path=ExceptionSourceLine}" Width="Auto"/>
<GridViewColumn Header="Exception Caller" DisplayMemberBinding="{Binding Path=ExceptionCaller}" Width="Auto"/>
<GridViewColumn Header="Date and Time Logged" DisplayMemberBinding="{Binding Path=dateLogged}" Width="Auto"/>
<GridViewColumn Header="View/Edit" Width="auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button x:Name="Open" Content="Open Or Edit"
Command="{Binding Path=DataContext.OpenExceptionCommand,RelativeSource={RelativeSource AncestorType=Window,Mode=FindAncestor}}"
CommandParameter="{Binding}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Button Content="Button" HorizontalAlignment="Left" Margin="237,138,0,0" Grid.Row="1" VerticalAlignment="Top" Command="{Binding RefreshDataGridCommand}"/>
</Grid>
</Grid>
我的xaml.cs文件
using ErrorLogger_WPF.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace ErrorLogger_WPF.Views
{
/// <summary>
/// Interaction logic for ExceptionDetailsListView.xaml
/// </summary>
public partial class ExceptionDetailsListView : Window
{
public ExceptionDetailsListView()
{
DataContext = new ExceptionDetailsListViewModel();
InitializeComponent();
}
}
}
我的视图模型
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using ErrorLogger_WPF.Services;
using ErrorLogger_WPF.Views;
using Shared;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ErrorLogger_WPF.ViewModels
{
internal partial class ExceptionDetailsListViewModel : ObservableObject
{
#region Properties
public ObservableCollection<ExceptionDetails> ExceptionDetails { get; set; }
#endregion
APIService apiService = new APIService();
public ExceptionDetailsListViewModel()
{
LoadData();
}
private async void LoadData()
{
var exceptionDetails = await apiService.GetExceptionDetails();
ExceptionDetails = new ObservableCollection<ExceptionDetails>(exceptionDetails);
}
[RelayCommand]
private void OpenException(ExceptionDetails exDetails)
{
var detailedExceptionView = new DetailedExceptionView(exDetails);
detailedExceptionView.ShowDialog();
}
[RelayCommand]
private void RefreshDataGrid()
{
LoadData();
}
}
}
1条答案
按热度按时间aamkag611#
您目前的实作在设定
ExceptionDetails
属性时不会引发INotifyPropertyChanged
。因此系结不会更新UI。请在setter中引发它,或使用内建的ObservableObject.SetProperty
。