WPF如何复制OpenFileDialog按钮和附带的文本字段
本文关键字:字段 文本 按钮 OpenFileDialog 何复制 复制 WPF | 更新日期: 2023-09-27 18:05:06
这是一个WPF应用程序的屏幕截图,我想复制的控件。我很难在谷歌上搜索我想要做的事情,因为我不确定描述这个过程的术语。在asp.net的世界里,我想要的是一个重复器控件。所有我想做的,是有一种方式,用户可以点击添加多个文件,他们想盖章。
下面是我想要重复的控件的截图,用红色圈出
浏览按钮将被设置为将文件路径推送到字符串的<list>
,然后再推送到一个字节数组,因为它被new PdfReader(System.IO.File.ReadAllBytes(filePaths[i]))
读入。
private void Button_Click(object sender, RoutedEventArgs e)
{
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".pdf";
dlg.Filter = "PDF Files (*.pdf)|*.pdf";
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
// Get the selected file name and display in a TextBox
if (result == true)
{
// set chosenFile variable
this.chosenFile = dlg.FileName;
inputBox.Text = chosenFile;
paths.Add(this.chosenFile);
}
}
这里是文本和按钮控件的xaml。
<Button x:Name="getPdfButton" Content="Browse" HorizontalAlignment="Left" Margin="421,65,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click" Height="23" RenderTransformOrigin="0.408,0.407"/>
<TextBox x:Name="inputBox" HorizontalAlignment="Left" Height="23" Margin="23,65,0,0" VerticalAlignment="Top" Width="393" TextChanged="inputBox_TextChanged"/>
使用ItemsControl和一个包含你想要重复的控件的DataTemplate。
将ItemsControl的ItemsSource绑定到一个ViewModel集合,该集合将处理每个文件选择
<ItemsControl ItemsSource="{Binding FileSelections}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
</Grid>
<TextBox Text="{Binding FilePath}" Margin="2"/>
<Button Command="{Binding BrowseFileCommand}" Margin="2" Grid.Column="1"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
在主ViewModel类中:
public ObservableCollection<FileSelection> FileSelections { get; set; }
在fileselectionviewmodel类:
public string FilePath
{
get
{
return _filePath;
}
set
{
_filePath = value;
RaisePropertyChanged("FilePath");
}
}
public ICommand BrowseFileCommand = new DelegateCommand(BrowseFile);
public void BrowseFile()
{
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".pdf";
dlg.Filter = "PDF Files (*.pdf)|*.pdf";
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
// Get the selected file name and display in a TextBox
if (result == true)
{
// set chosenFile variable
this.FilePath = dlg.FileName;
}
}
可以填空....
添加一个ListBox
控件,当用户选择一个文件时,将其添加到ListBox.Items
?