在WPF中显示Grid中的附加文件路径

时间:2012-12-18 12:08:50

标签: wpf

我正在开发WPF应用程序。

请检查此图片:

screenshot

当我点击+符号按钮时,我想在下面的网格中显示附加文件路径。 为此,我做了一些工作,我将在下面向您展示。

XAML文件

<StackPanel>
    <StackPanel Orientation="Horizontal">
        <Appctr:GridToolBar x:Name="attachmentTool" Grid.Row="0" RemoveRowClick="attachmentTool_RemoveRowClick" AddRowClick="attachmentTool_AddRowClick" MoveUpClick="attachmentTool_MoveUpClick" MoveDownClick="attachmentTool_MoveDownClick" ToolBtn_Search="ButtonEditSettings_attachmentUsedClick" Height="25" />
    </StackPanel>
    <dxg:GridControl x:Name="dxgAttachment" Height="200" AutoPopulateColumns="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
        <dxg:GridControl.View>
            <dxg:TableView x:Name="vwAttachment" ShowGroupPanel="False" AllowBestFit="True" AutoWidth="True">
            </dxg:TableView>
        </dxg:GridControl.View>
        <dxg:GridControl.Columns>
            <!--<dxg:GridColumn FieldName="SlNo" Header="Sl #"></dxg:GridColumn>-->
            <dxg:GridColumn FieldName="LocalPath" Header="Attachment Path" Width="Auto" ReadOnly="True" EditSettings="{dxe:TextSettings TextWrapping=Wrap}"></dxg:GridColumn>
            <!--<dxg:GridColumn FieldName="LocalPath" Header="Attachment Path" Width="1130" ReadOnly="True" EditSettings="{dxe:TextSettings TextWrapping=Wrap}"></dxg:GridColumn>-->
        </dxg:GridControl.Columns>
    </dxg:GridControl>
</StackPanel>

.CS文件

private void attachmentTool_AddRowClick(object sender, RoutedEventArgs e)
{
    Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

    // Set filter for file extension and default file extension
    //dlg.DefaultExt = ".txt";
    //dlg.Filter = "Text documents (.txt)|*.txt";

    // Display OpenFileDialog by calling ShowDialog method
    Nullable<bool> result = dlg.ShowDialog();

    // Get the selected file name and display in a TextBox
    if (result == true)
    {
        try
        {
            // Open document
            string filePath = dlg.FileName;
            AttachmentInfo info = new AttachmentInfo();
            info.LocalPath = filePath;
            info.FileName = System.IO.Path.GetFileNameWithoutExtension(filePath);
            info.FileExtension = System.IO.Path.GetExtension(filePath).Replace(".", "");
            Stream strm = dlg.OpenFile();
            byte[] data = new byte[strm.Length];
            strm.Read(data, 0, data.Length);
            info.AttachmentContent = data;
            strm.Close();
            strm.Dispose();
            this.dxgAttachment.ItemsSource = info;

        }
        catch (Exception ex)
        {
            DXMessageBox.Show("Unable to attach file. " + ex.Message);
        }
    }
}

问题

  1. 如何在网格中显示附加文件路径?

  2. 附加单个或多个文件后,我也想保存数据库中的所有文件。怎么做?

  3. 通过使用上面的代码,我无法在网格中显示附加文件列表。

1 个答案:

答案 0 :(得分:0)

我不知道你正在使用什么类型的DataGrid,但ItemsSource可能应该是IEnumerable(项目集合),而不仅仅是单个项目。

试试这个:

this.dxgAttachment.ItemsSource = new List<AttachmentInfo>() { info };