Search code examples
c#windowslistviewwindows-store-apps

Get the display name of the file in the listview in windows store application


I am trying to display the name of the file which is clicked by the user in the listview but the problem is it is displaying the type of item clicked i.e. Windows.Storage.StorageFile clicked.

Code:

<Page
    x:Class="Notes.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Notes"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <GridView Background="White">


        <Button x:Name="Button_Create" Content="Create" Background="Black" Height="104" Width="188" FontSize="32" Click="ButtonCreate_Click"/>
        <ListView IsItemClickEnabled="True" x:Name="filesListView" Foreground="Gray" HorizontalAlignment="Left" SelectionMode="None" ItemClick="filesListView_ItemClick">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding DisplayName}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </GridView>

</Page>

and the code for MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;

using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.BulkAccess;
using Windows.Storage.Streams;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
//using System.Deployment.Application;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Notes
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            //getFolders();
            getFoldersFrommCurrentDirectory();
        }

        private async void ButtonCreate_Click(object sender, RoutedEventArgs e)
        {
            StorageFolder folder = await KnownFolders.MusicLibrary.CreateFolderAsync("Notes", CreationCollisionOption.OpenIfExists);

            //create a file in that folder

            StorageFile file = await folder.CreateFileAsync("MyNotes.txt", CreationCollisionOption.GenerateUniqueName);

            //write to that file

            await Windows.Storage.FileIO.WriteTextAsync(file, "My notes file");

            getFoldersFrommCurrentDirectory();
        }

        private async void getFoldersFrommCurrentDirectory()
        {
            //get folders from current application folder
            StorageFolder folder = await KnownFolders.MusicLibrary.CreateFolderAsync("Notes", CreationCollisionOption.OpenIfExists);
            IReadOnlyList<StorageFile> files = await folder.GetFilesAsync();
            filesListView.ItemsSource = files;
        }




        private async void filesListView_ItemClick(object sender, ItemClickEventArgs e)
        {
            var message = new MessageDialog(e.ClickedItem+ "clicked", "listview item clicked");
            await message.ShowAsync();
        }


        //private async void getFolders()
        //{
        //    //get a folder from known folders
        //    IReadOnlyList<StorageFolder> folders = await KnownFolders.MusicLibrary.GetFoldersAsync();
        //    filesListView.ItemsSource = folders;
        //}


    }


}

Solution

  • Try changing filesListView_ItemClick to

    var name = (sender as StorageFile).Name;
    var message = new MessageDialog(name + "clicked", "listview item clicked");
    await message.ShowAsync();