将ArrayList设为wpf的数据源

本文介绍如何在WPF中使用DataGrid显示文件列表,并通过DataGridHyperlinkColumn实现文件的超链接点击打开。文章详细展示了XAML布局代码与C#后端代码,包括设置DataGrid的样式、绑定数据源以及处理点击事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、xaml如下:

<Window x:Class="WpfDataGridLink.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <DataGrid Height="287" HorizontalAlignment="Left" Margin="12,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="479" HeadersVisibility="None" Background="{x:Null}" Foreground="#FF3939EB" Hyperlink.Click="dataGrid1_Click" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridHyperlinkColumn Header="Header" Binding="{Binding}" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

注意:此处AutoGenerateColumns="False"
2、代码如下:

 private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ArrayList fileList = new ArrayList();

            fileList.Add("D://openFile//anote.xml");
            fileList.Add("D://openFile//UserConfig.xml");
            fileList.Add("D://openFile//搜索与主机属性.pdf");
            dataGrid1.ItemsSource = fileList;
        }

 

        private void dataGrid1_Click(object sender, RoutedEventArgs e)
        {
            if (dataGrid1.SelectedItem == null)
            {
                return;
            }
            string fileName = dataGrid1.SelectedItem.ToString();
            if (System.IO.File.Exists(fileName))
            {
                Process.Start(fileName);
            }
        }

转载于:https://www.cnblogs.com/liancs/archive/2011/06/17/3879310.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值