【问题标题】:ListView not showing contents of BindingCollectionListView 不显示 BindingCollection 的内容
【发布时间】:2021-06-27 04:03:28
【问题描述】:

我可能查看了一百个不同的堆栈溢出问题/答案,我的代码似乎与大多数答案完全相同相同,但我仍然无法让 ListView 显示任何内容。

在我的MainWindow.cs 我有

api = new Api();
            
MainViewModel = new MainViewModel();
MainViewModel.Loading = true;
DataContext = MainViewModel;

AttemptLogin();
if (Account.IsLoggedIn){
    api.getAccountDetails();
    
    MainViewModel.GetProfiles(api);
    InitializeComponent();
    
}

我的MainViewModel 包含:

private ObservableCollection<Profile> _profiles;
public ObservableCollection<Profile> Profiles => _profiles;

...

public void GetProfiles(Api api){
    _profiles = new ObservableCollection<Profile>(api.getProfiles());
}

在我的 xaml 中,我有:


<Window.DataContext>
    <viewModels:MainViewModel />
</Window.DataContext>

<ListView
    ItemsSource="{Binding Profiles}">
    <ListView.View>
        <GridView>
            <GridViewColumn
                DisplayMemberBinding="{Binding Name}"
                Header="Name" />
            
        </GridView>
    </ListView.View>
</ListView>

API 调用是:

public ObservableCollection<Profile> getProfiles(){
    var request = new RestRequest("api/benchmarks/results", Method.POST);
    var response = _restClient.Execute(request);
    dynamic test = JObject.Parse(response.Content);
    ObservableCollection<Profile> Profiles = new ObservableCollection<Profile>();
    for (var i = 0; i < test.result.Count; i++){
        Profiles.Add(new Profile(test.result[i]));
    }
    return Profiles;
}

我还有其他属性显示在来自MainViewModel 的 xaml 中。如果我在InitializeComponent 上设置断点,我可以看到MainViewModel.Profiles 包含我希望它包含的内容。

然而,我得到的只是名称标题,没有内容。

我在这里错过了什么?

编辑:

如果我将 OnPropertyChanged 添加到 Profile 的设置器并相应地调整我的视图模型:

...

public ObservableCollection<Profile> Profiles{
    get => _profiles;
    set{
        _profiles = value;
        OnPropertyChanged("Profiles");
    }
}

...

public void GetProfiles(Api api){
    Profiles = new ObservableCollection<Profile>(api.getProfiles());
}

没有变化。

【问题讨论】:

  • 在这种情况下,您是否尝试在 ViewModel 上添加 INotifyPropertyChanged 并在公共属性“Profiles”的 setter 方法上调用 OnPropertyChanged 事件。试试看吧。
  • @GK 我确实有。我已经用我添加它的方式更新了这个问题。我的视图模型上已经有INotifyPropertyChanged,因为其他属性使用它没有任何问题。
  • 另外,不应该使用ObservableCollection 否定OnPropertyChanged 的需要吗?这不是使用它的要点之一吗?
  • 您可以尝试在 InitializeComponent 之后设置 DataContext 吗? "DataContext = MainViewModel;"
  • 另外,还有一个建议,尝试在 InitializeComponent() 方法之后做所有事情。这是一般的正确做法,您可以避免所有这些小问题。

标签: c# wpf observablecollection


【解决方案1】:

尝试在 InitializeComponent() 方法之后设置 DataContext。还有一个建议,将 InitializeComponent() 移出 if 块。始终远离条件块,让 UI 自行加载,您可以决定在页面/窗口加载后要执行的操作或显示的内容。

这是修改后的代码,

public MainWindow()
{
  InitializeComponent(); // Keep this as first statement in your View constructor
  // Move below code to some private method and make a call from here. 
  // Just code looks simpler and clean.
  InitializeViewModel();
}

private void InitializeViewModel()
{
  api = new Api();
  MainViewModel = new MainViewModel();
  MainViewModel.Loading = true;
  DataContext = MainViewModel;
  AttemptLogin();
  if (Account.IsLoggedIn)
  {
    api.getAccountDetails();
    MainViewModel.GetProfiles(api);
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多