简体   繁体   English

如何使用WPF / c#将快捷方式文件夹显示到DataGrid或其他控件中

[英]How to display a folder of shortcuts into a DataGrid or another control using WPF/c#

I have a folder (X:\\Shortcuts) with shortcuts which could have the following extensions 我有一个包含快捷方式的文件夹(X:\\ Shortcuts),其扩展名为

 LNK, URL, EXE

I want to show a grid of the shortcuts (Name and Icon) so the user can select them and they will get copied to H:\\Desktop if it exists otherwise %USERPROFILE%\\Desktop 我想显示快捷方式(名称和图标)的网格,以便用户可以选择它们,如果存在,它们将被复制到H:\\ Desktop ,否则为%USERPROFILE%\\ Desktop

I don't need help with copying the file. 我不需要复制文件的帮助。 I need help with displaying the contents of a folder as a Grid in my WPF application 我需要在WPF应用程序中以网格形式显示文件夹内容的帮助

Someone referred me to http://www.wpfsharp.com/2012/10/23/displaying-images-from-a-folder-with-details-in-wpf/ 有人把我引到http://www.wpfsharp.com/2012/10/23/displaying-images-from-a-folder-with-details-in-wpf/

but this assumes the folder is filled with images. 但这是假设文件夹中充满了图像。


My Code-behind code 我的代码隐藏代码

    private ObservableCollection<Shortcut> shortcutItems;
    public ObservableCollection<Shortcut> ShortcutItems
    {
        get { return shortcutItems; }
        set { shortcutItems = value; }
    }

    private ObservableCollection<Shortcut> GetIcons()
    {
        if (shortcutItems == null)
            shortcutItems = new ObservableCollection<Shortcut>();
        shortcutItems.Clear();

        foreach (var item in Directory.GetFiles(path).Where(x => x.EndsWith(".lnk")))
        {
            var icc = Icon.ExtractAssociatedIcon(item);
            shortcutItems.Add(new Shortcut()
            {
                Name = System.IO.Path.GetFileName(item.Substring(0, item.Length - 4)),
                BitMapIcon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(icc.Handle,
                             System.Windows.Int32Rect.Empty,
                             System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions())
            });
        }
        return shortcutItems;
    }

    private void getShortCuts()
    {
        shortcutItems = GetIcons();
    }

My Entire XAML 我的整个XAML

<UserControl x:Class="Camden_Automated_Help_Desk.AvailableShortcuts"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Camden_Automated_Help_Desk"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <DataGrid ItemsSource="{Binding ShortcutItems}"  AutoGenerateColumns="False"  >
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Icons">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding BitMapIcom}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
            </DataGrid.Columns>    
        </DataGrid>

        <Button x:Name="buttonCopy" Content="Copy Shortcuts" HorizontalAlignment="Left" Margin="165,265,0,0" VerticalAlignment="Top" Width="125" FontSize="16"/>
        <Button x:Name="buttonReset" Content="Refresh" HorizontalAlignment="Left" Margin="10,265,0,0" VerticalAlignment="Top" Width="125" FontSize="16" Click="buttonReset_Click"/>
    </Grid>
</UserControl>

I see the header showing the word Icon and label Name but I don't see the Icons or the Name of the Icons in the DataGrid 我看到标题显示图标Icon和标签名称,但是在DataGrid中看不到图标或图标名称

you need to extract the icons from the shortcut/file. 您需要从快捷方式/文件中提取图标。

I made the following Shortcut class that has a name property and a bitmap(image) property. 我制作了以下具有name属性和bitmap(image)属性的Shortcut类。

public class Shortcut
{
    public Shortcut()
    {

    }

    public string Name { get; set; }


    public BitmapSource BitMapIcom { get; set; }
}

In my MainViewModel declared an ObservableCollection of Shortcuts called ShortcutItems and use the following method to populate the collection with 'items' from a folder. 在我的MainViewModel中,声明了一个名为ShortcutItems的ObservableCollection of Shortcuts,并使用以下方法使用文件夹中的“ items”填充集合。

private ObservableCollection<Shortcut> shortcutItems;
    public ObservableCollection<Shortcut> ShortcutItems
    {
        get { return shortcutItems; }
        set { shortcutItems = value; }
    }



private ObservableCollection<Shortcut> GetIcons()
    {
        if (shortcutItems == null)
            shortcutItems = new ObservableCollection<Shortcut>();
        shortcutItems.Clear();

        foreach (var item in Directory.GetFiles(@"C:\Users\albErt\Desktop").Where(x => x.EndsWith(".lnk")))
        {
            var icc = Icon.ExtractAssociatedIcon(item);
            shortcutItems.Add(new Shortcut()
            {
                Name = System.IO.Path.GetFileName(item.Substring(0, item.Length - 4)),
                BitMapIcom = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(icc.Handle,
                             System.Windows.Int32Rect.Empty,
                             System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions())
            });
        }
        return shortcutItems;
    }

And in the viewmodel constructor just ShortcutItems = GetIcons(); 在viewmodel构造函数中,只是ShortcutItems = GetIcons(); Make sure your directory path is correct. 确保您的目录路径正确。 It loops through all the files that have the extension ".lnk"(shortcuts). 它遍历所有扩展名为“ .lnk”(快捷方式)的文件。 Extracts the icon from the path and converts it to a bitmap. 从路径中提取图标并将其转换为位图。 It's not perfect but it retrieves the icons. 它不是完美的,但是会检索图标。 I used a DataGrid to test if the icons are displayed. 我使用了DataGrid来测试是否显示了图标。 If you use a datagrid just add the ObservableCollection as the Itemssource and create a templatecolumn for the image: This datagrid is only thing in my XAML, other than this I set the datacontext behind code. 如果使用数据网格,只需将ObservableCollection添加为Itemssource并为图像创建一个模板列:此数据网格仅是我的XAML中的内容,除此之外,我还在代码后面设置了数据上下文。

    <DataGrid ItemsSource="{Binding ShortcutItems}"  AutoGenerateColumns="False"  >
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Icons">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Image Source="{Binding BitMapIcom}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
        </DataGrid.Columns>    

This is how using listview would be : 这就是使用listview的方式:

<ListView ItemsSource="{Binding ShortcutItems}" >
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Icon">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding BitMapIcom}" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
            </GridView>
        </ListView.View>
    </ListView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM