如何在鼠标双击上获取列表视图上所选项目的绝对路径

时间:2013-10-28 09:54:15

标签: c# wpf listview

我在listview中添加了一些文件。我希望鼠标双击时获取列表视图中项目的绝对路径。下面是我用来检索项目并将它们添加到listview的代码和xaml

代码:

public class Documents
{
    public string Name
    {
        get;
        set;
    }

    public ImageSource Image
    {
        get;
        set;
    }
}

//Code to retrieve documents

        String docpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        string[] docnames = System.IO.Directory.GetFiles(docpath);

        foreach (string docname1 in docnames)
        {                
            System.Drawing.Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(docname1);
            string docname = Regex.Match(docname1, @".*\\([^\\]+$)").Groups[1].Value;

            System.Windows.Controls.Image image = new System.Windows.Controls.Image();

            int fileExtPos = docname.LastIndexOf(".");
            if (fileExtPos >= 0)
                docname = docname.Substring(0, fileExtPos);

            FileInfo fileExt;
            fileExt = new FileInfo(docname1);
            if (fileExt.Extension.ToLower() == ".doc" || fileExt.Extension.ToLower() == ".docx" || 
                fileExt.Extension.ToLower() == ".xls" || fileExt.Extension.ToLower() == ".xlsx" || 
                fileExt.Extension.ToLower() == ".txt" || fileExt.Extension.ToLower() == ".rtf" || 
                fileExt.Extension.ToLower() == ".pdf" || fileExt.Extension.ToLower() == ".dot" || 
                fileExt.Extension.ToLower() == ".html" || fileExt.Extension.ToLower() == ".odt" || 
                fileExt.Extension.ToLower() == ".odm" || fileExt.Extension.ToLower() == ".ott" || 
                fileExt.Extension.ToLower() == ".info" || fileExt.Extension.ToLower() == ".xml" || 
                fileExt.Extension.ToLower() == ".xps" || fileExt.Extension.ToLower() == ".xhtml" || 
                fileExt.Extension.ToLower() == ".omm" || fileExt.Extension.ToLower() == ".csv")
            {

                files.Add(new Documents()
                {
                    Name = docname,
                    Image = icon.ToImageSource()
                });
            }
        }

XAML:

<GroupBox Grid.Column="1" Header="My Documents" HorizontalAlignment="Left" Margin="10,0,0,10" Width="200">
        <ListView x:Name="DocsListView" HorizontalAlignment="Left" Height="500" VerticalAlignment="Top" Width="175" Margin="5,5,5,5" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="True" ItemsSource="{Binding docs, ElementName=MyWindow}" >
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="File" Width=" auto">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Image Source="{Binding Image}" />
                                </Grid>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Name" Width="auto" DisplayMemberBinding="{Binding Name}" />
                </GridView>
            </ListView.View>
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">

                    <EventSetter Event="MouseDoubleClick" Handler="ListView_MouseDClick" />

                </Style>
            </ListView.ItemContainerStyle>
        </ListView>
    </GroupBox>

2 个答案:

答案 0 :(得分:1)

使用您使用的FullName对象的FileInfo属性。我返回文件的完整路径。

如果您稍后需要在应用程序中使用该路径,则应使用属性扩展Documents类以存储文件的路径。

答案 1 :(得分:0)

只需使用

增强您的Documents类
public string Path
{
    get;
    set;
}

在您的方法中,使用以下命令创建Documents实例:

files.Add(new Documents()
{
    Name = docname,
    Image = icon.ToImageSource(),
    Path = docname1
});

在事件处理程序ListView_MouseDClick中,您应该能够获取Documents实例并使用Path属性。