0

I'm writing simple WPF Application and I wanted to use ListView to display List of items. My code is:

WPF.xaml

<ListView Grid.Column="0" Grid.Row="1" Margin="10,0,10,5" ItemsSource="{Binding MyCollection.Elements}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding ElementDescriptions}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

WPF.xaml.cs

public MyViewModel ViewModel
    {
        get { return DataContext; }
        set { DataContext = value; }
    }

MyViewModel.cs

public OwnedCollection Elements { get; set; }

OwnedCollection.cs

public List<ElementDescriptions> ElementDescriptions { get; set; }

I'm 100% sure, that communication between View and ViewModel is correct, because displaying simple message doesn't make me troubles. Am I doing right binding in ListView?

spajce
  • 7,044
  • 5
  • 29
  • 44
Fka
  • 6,044
  • 5
  • 42
  • 60

1 Answers1

0

A couple things:

First,

TextBlock Text="{Binding ElementDescriptions}"

doesn't make a lot of sense because ElementDescriptions is a collection. If you want to loop through all the ElementDescriptions in your List you should really be binding the ItemSource of the ListView to ElementDescriptions then accessing some text property of the ElementDescriptions class:

<ListView Grid.Column="0" Grid.Row="1" Margin="10,0,10,5" ItemsSource="{Binding MyCollection.ElementsElementDescriptions }">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding ElementDescriptions.SomeTextField}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Second, are you using INotifyPropertyChanged so the view knows to update? More info on that here: OnPropertyChanged with a List

Community
  • 1
  • 1
lloyd christmas
  • 556
  • 1
  • 4
  • 16