WPF 绑定到 DataGrid 可以部分生成列标题,但不会列出项目

问题描述 投票:0回答:1

我正在以 mvvm 风格和对象工厂构建一个应用程序,至于我正在研究的背景,那么相当新。

抱歉,我的学习部分代码是用德语编写的。

问题

我尝试将

ObservableCollection<Betrag> BeitragsListe
绑定到 dataGrid,但在查看结果时,它会按预期生成列标题,但它不会填充网格中的单个项目,因此
DataContext
Itemsource
是正确的至少是这么想的。

查看:

<Window x:Class="Essensausgleich.contributionWindow"
        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:Essensausgleich.ViewModel"
        mc:Ignorable="d"
        Title="contributionWindow" Height="235" Width="382" >
    <Grid>
        <DataGrid
            Name="DataGridContribution"
            ItemsSource="{Binding BeitragsListe}"
            AutoGenerateColumns="True"
            />
    </Grid>
</Window>

查看模型又名

Anwendung
(仅相关部分):

属性——

OnProperty
OnCollection
是因为尝试过才出现的:

public ObservableCollection<Betrag> BeitragsListe
{
     get => this._BeitragsListe;
     set
     {
         this._BeitragsListe = value;
         OnPropertyChanged();
         OnCollectionChanged(
new System.Collections.Specialized.
NotifyCollectionChangedEventArgs(
System.Collections.Specialized.
NotifyCollectionChangedAction.Reset));
         Log.WriteLine($"{BeitragsListe.GetType().Name} has changed");
     }
}

private ObservableCollection<Betrag> _BeitragsListe = new ObservableCollection<Betrag>();

使用方法

/// <summary>
/// Opens The window and fills the Datagrid with the Current
/// selectedInhabitant to display the total Expenses
/// </summary>
public void OpenContributioWindow()
{
     Log.WriteLine("ButtonAuflistung Clicked");
     var vm = this.Kontext.Produziere<ViewModel.Anwendung>();

     vm.Anzeigen<contributionWindow>();

     if (!string.IsNullOrEmpty(InhabitantsSelected))
     {
         if (InhabitantsSelected == Bewohner1Name)
         {       
             this.BeitragsListe = new ObservableCollection<Betrag>(bewohner1.Einzelbetraege);         
         }
         else if (InhabitantsSelected == Bewohner2Name)
         {
             this.BeitragsListe = new ObservableCollection<Betrag>(bewohner2.Einzelbetraege);
         }
         else
         {
             Log.WriteLine($"No Inhabitant selected or not found, Selcted:{InhabitantsSelected}");
         }
     }
}

Struct

Betrag
- 添加到列表中:

/// <summary>
/// struct to serve as single entry for the Bewohner object
/// </summary>
public struct Betrag
{
     /// <summary>
     /// Category string of Betrag
     /// </summary>
     public string kategorie {  get; set; }

     /// <summary>
     /// Amount in decimal of Betrag
     /// </summary>
     public decimal wert {  get; set; }

     /// <summary>
     /// constructor for the Betrag Struct
     /// </summary>
     /// <param name="k"></param>
     /// <param name="w"></param>
     public Betrag(string k, decimal w)
     {
         kategorie = k;
         wert = w;
     }       
}

调试时,我检查了

BeitragsListe
是否具有预期的条目,例如
BeitragsListe[0]
是一个由
Betrag
组成的
wert=10, kategorie="Hofa"
对象 //一个是十进制,另一个是字符串

xamlLiveView

ContributionWindow with Headers without values

DebugInfoBeitragsListe

c# mvvm data-binding datagrid observablecollection
1个回答
0
投票

尝试直接绑定到 Grid 的 DataContext。

在您的

OpenContributioWindow()
函数中,设置
this.BeitragsListe
后,尝试添加
DataGridContribution.DataContext = this.BeitragsListe;
(假设此代码位于您的 MainWindow.xaml.cs 文件或等效文件中)。这应该将数据绑定到网格并在屏幕上显示适当的值。

© www.soinside.com 2019 - 2024. All rights reserved.