0

我正在开发一个 WPF 应用程序。

请检查此图像:

截屏

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

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. 附加单个或多个文件后,我还想将所有文件保存在数据库中。这个怎么做?

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

4

1 回答 1

0

我不知道您使用的是哪种 DataGrid,但 ItemsSource 可能应该是一个 IEnumerable(项目的集合),而不仅仅是一个项目。

尝试这个:

this.dxgAttachment.ItemsSource = new List<AttachmentInfo>() { info };
于 2012-12-19T23:49:10.917 回答