我正在开发WPF应用程序。
请检查此图片:
当我点击+符号按钮时,我想在下面的网格中显示附加文件路径。 为此,我做了一些工作,我将在下面向您展示。
<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>
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);
}
}
}
如何在网格中显示附加文件路径?
附加单个或多个文件后,我也想保存数据库中的所有文件。怎么做?
通过使用上面的代码,我无法在网格中显示附加文件列表。
答案 0 :(得分:0)
我不知道你正在使用什么类型的DataGrid,但ItemsSource可能应该是IEnumerable(项目集合),而不仅仅是单个项目。
试试这个:
this.dxgAttachment.ItemsSource = new List<AttachmentInfo>() { info };