0

I've been trying to follow examples and other peoples questions but I can't get this list view to bind to an ObservableCollection. I can't figure out what I'm doing wrong. Would appreciate if somebody can point out my mistake please.

MainWindow.xaml:

<Window x:Class="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:Duplicate_File_Finder"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView ItemsSource="{Binding Path=dFileList}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="File Name" DisplayMemberBinding="{Binding Path=FileName}" />
                    <GridViewColumn Header="Path" DisplayMemberBinding="{Binding Path=Path}" />
                </GridView>
            </ListView.View>
        </ListView>

    </Grid>
</Window>

MainWindow.xaml.vb:

Imports System.Collections.ObjectModel

Public Class DuplicateFile
    Public Sub New(ByVal FileName As String, ByVal Path As String, ByVal Hash As String, ByVal Icon As String)
        Me.FileName = FileName
        Me.Path = Path
        Me.Hash = Hash
        Me.Icon = Icon
    End Sub

    Public Property Icon As String
    Public Property FileName As String
    Public Property Path As String
    Public Property Hash As String
End Class

Class MainWindow

    Public dFileList As New ObservableCollection(Of DuplicateFile)

    Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded

        For i = 0 To 1
            dFileList.Add(New DuplicateFile("File " + i.ToString, "C:\File " + i.ToString, i.ToString, i.ToString))
        Next

    End Sub
End Class
5
  • How are you setting your DataContext of MainWindow? You could do it 1 of 2 ways: 1) set this.DataContext = this, or 2) Set and x:Name of your MainWindow element and then use ElementName= to it, in order to use it as the context for your binding. Commented May 20, 2017 at 20:19
  • I wasn't :) But I've added "Me.DataContext = Me" to the MainWindow's Loaded event and now I'm getting "BindingExpression path error: 'dFileList' property not found on 'object' ''MainWindow'" Commented May 20, 2017 at 20:33
  • Sorry, I was stuck in C# lang with the "this" thing! (I meant Me). It's because dFileList is a field, not a property. Try setting it to a property (like you have with properties in the DuplicateFile class) - Public Property dFileList As New ObservableCollection.... Commented May 20, 2017 at 20:37
  • No problem lol. That worked thank you :) Commented May 20, 2017 at 20:40
  • Awesome. Glad to have helped. Have posted as an answer if you'd like to check it out :) Commented May 20, 2017 at 20:42

1 Answer 1

1

First, you need to ensure your dFileList is a property, not a field, (which it currently is):

Public Property dFileList As New ObservableCollection(Of DuplicateFile)

You also need to tell your Window what to use as a DataContext.

You can do this in the InitializeComponent or Loaded subroutine:

Me.DataContext = Me

Or in your XAML, set an x:Name for the Window, such as x:Name="mainWindow".

Then set the ListView's Binding to refer to the element for the right context: ItemsSource="{Binding Path=dFileList, ElementName=mainWindow}".


Your bindings should then work perfectly :)

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.