I am using Visual Studio 2022 to make a WPF application that utilises MVVM. I have an observable collection, I have INotifyPropertyChanged implemented, I am binding everything as correctly as I can and have googled a plenty. I'm convinced I have everything correctly and yet the listview won't access the attributes of the items. The listview reflects the number of items that are in the collection but shows a binding error that says the property Autor was not found on the object Kniha. Please excuse my non-English naming, it is for a project where this is the convention and I don't want to rename them just for the sake of this question.
My ListView in MainWindow.xaml:
<Window x:Class="DBKnihovna.MainWindow"
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:DBKnihovna"
xmlns:converters="clr-namespace:DBKnihovna.ViewModel.ValueConverters"
xmlns:vm="clr-namespace:DBKnihovna.ViewModel"
xmlns:v="clr-namespace:DBKnihovna.View"
xmlns:controls="clr-namespace:DBKnihovna.View.Zalozky"
mc:Ignorable="d"
WindowStartupLocation="CenterScreen"
Title="DBKnihovna" Height="600" Width="800">
<Window.Resources>
<converters:MultivalueConverterProCommandy x:Key="commandParamConverter" />
<vm:MainVM x:Key="MainVM" />
<DataTemplate DataType="{x:Type vm:MainVM}">
<v:NeprihlasenyUC />
</DataTemplate>
<vm:KnihovnikuvVM x:Key="KnihovnikuvVM" />
<DataTemplate DataType="{x:Type vm:KnihovnikuvVM}">
<v:KnihovnikuvUC />
</DataTemplate>
</Window.Resources>
<Grid DataContext="{StaticResource MainVM}">
<ListView Name="lvVsechnyKnihy" Grid.Row="0" Grid.Column="0"
ItemsSource="{Binding VsechnyKnihy}"
SelectedValue="{Binding AktualniKniha, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding AktualniKniha}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Autor}" Header="Autor"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
My MainVM:
public class MainVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
/* ... */
private ObservableCollection<Kniha> vsechnyKnihy;
public ObservableCollection<Kniha> VsechnyKnihy
{
get { return vsechnyKnihy; }
set
{
vsechnyKnihy = value;
OnPropertyChanged(nameof(VsechnyKnihy));
}
}
private Kniha aktualniKniha;
public Kniha? AktualniKniha
{
get { return aktualniKniha; }
set
{
aktualniKniha = value;
OnPropertyChanged(nameof(AktualniKniha));
}
}
/* ... */
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
My class Kniha:
public class Kniha
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string ID;
public string Nazev;
public string Autor;
public int RokVydani;
public int PocetStran;
public int PocetVytisku;
public byte[] Obrazek;
public Kniha(string nazev, string autor, int rok, int pocetStran, int pocetVytisku, byte[] obrazek, string id = "")
{
ID = id == "" ? new BsonID().Id.ToString() : id;
Nazev = nazev;
Autor = autor;
RokVydani = rok;
PocetStran = pocetStran;
PocetVytisku = pocetVytisku;
Obrazek = obrazek;
}
}
Visually the result looks like this: enter image description here I debugged and all the objects in the collection DO have the value assigned.
What do I need to do differently to see the value of Kniha.Autor in the column Autor?