0

I'm looking for examples or help to create a WPF listview of files.

<ListView Margin="10,10,0,13" Name="ListView1" HorizontalAlignment="Left"
         VerticalAlignment="Top" Width="194" Height="200">

I Load my files with this method :

    private void AddFiles_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Multiselect = true;

        if (ofd.ShowDialog() == true)
        {
            string[] filePath = ofd.FileNames;
            string[] safeFilePath = ofd.SafeFileNames;
        }
    }

What should I do now ?

ListView1.Items.Add(...) don't seems to work. In fact I can't find ListView1 from my cs code.

I found info here

3
  • 1
    do you want to show all files in a folder or only files selected by the user one by one? what is your end result? Commented Sep 29, 2011 at 12:52
  • The problem is not about loading the files. My problem concern adding something to a listview. Commented Sep 29, 2011 at 12:54
  • About the name, try x:Name instead of Name. Commented Sep 29, 2011 at 13:03

3 Answers 3

2

I would recommend to use DataBinding for display items in ListView you should bind the ObservableColliction files; with your ListView ItemSource property and when you add or remove files in collection on the ListView items will be updated automaticaly

for example look at this article

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

Comments

1

Simple,

  • Store your file(name)s in a list (ObservableCollection) in your ViewModel
  • Databind the ListView.ItemSource to that collection
  • Add/Remove/Change files in the Collection, not in the Listview

If you're not using an explicit ViewModel, use your WindowClass as such.

Comments

1

as a quick and dirty way you can assign the collection of files directly to the ItemsSource property of the ListView

ListView1.ItemsSource = safeFilePath;

in the XAML you can add an ItemTemplate to customize the visualization of the single files

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.