Dev
Dev

Reputation: 1826

How to bind a list view in a data template from a view model in WPF

I made an app with a list view binding with the data being process in a class library So the data model resides in the class library. As an extension, I wanted to use AvalonDock to manage my windows. Following an example, the way in which they bind data is through data template which is passed through a data template from a view model. So I remove their text box and put my listview but I can no longer access the list view from c# code using the listview item source because I have put it in the data template. So in the constructor, of the fest view model I create a new instance of the list type and assign the value but no data comes through to the view. I step through the festviewmodel and the data is there in the list that I created in the ViewModel is no data to the view. Just out of curiosity I have created a text block and set the value in the constructor and it shows on the view but I the data from the List to the Listview is empty.

<localController:PanesTemplateSelector.festViewTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <ListView x:Name="lvfest"  >
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Width="100" DisplayMemberBinding="{Binding Fest.id}" >
                                <GridViewColumn.Header>
                                    <GridViewColumnHeader Tag="ID" Click="lvfestColumnHeader_Click">ID</GridViewColumnHeader>
                                </GridViewColumn.Header>
                            </GridViewColumn>
                            <GridViewColumn  Width="100" DisplayMemberBinding="{Binding Fest.format}" >
                                <GridViewColumn.Header>
                                    <GridViewColumnHeader Tag="Format" Click="lvfestColumnHeader_Click">Format</GridViewColumnHeader>
                                </GridViewColumn.Header>
                            </GridViewColumn>
                            <GridViewColumn Width="100" DisplayMemberBinding="{Binding Fest.modified}" >
                                <GridViewColumn.Header>
                                    <GridViewColumnHeader Tag="Date" Click="lvfestColumnHeader_Click">Date</GridViewColumnHeader>
                                </GridViewColumn.Header>
                            </GridViewColumn>
                        </GridView>
                    </ListView.View>
                </ListView>
            </StackPanel>
        </DataTemplate>
    </localController:PanesTemplateSelector.festViewTemplate>


public class festViewModel
{
    public festViewModel() : base("fest Stats")
    {
        Workspace.This.ActiveDocumentChanged += new EventHandler(OnActiveDocumentChanged);
        Fest = new List<festItem>();
        Fest = MainWindow._fest.festData.fest_Items;
    }
    public List<festItem> Fest;
}

Upvotes: 1

Views: 1212

Answers (1)

mm8
mm8

Reputation: 169390

You could bind the ItemsSource property of the ListView to a property of its DataContext. So if the DataContext is a festViewModel instance, you could bind it like this:

<ListView x:Name="lvfest" ItemsSource="{Binding Fest}">
...

...provided that Fest is a public property of the festViewModel class:

public List<festItem> Fest { get; set; }

You cannot bind to fields.

Upvotes: 2

Related Questions