WPF how to duplicate a OpenFileDialog button and accompanying text field

时间:2015-07-28 16:29:38

标签: c# wpf xaml

Here is a screenshot of a WPF app with the controls that i want to duplicate. Im having a difficult time googling what i want done, because im not sure of the terminology that describes the process. In the asp.net world, what i'd be after is a repeater control. All i want to do, is have a way that the user can click to add multiple files they want to stamp.

Here is a screenshot with the control i want to repeat circled in red

The browse button will be setup to push the file path to a <list> of strings, then later to a byte array as its being read in with new PdfReader(System.IO.File.ReadAllBytes(filePaths[i])).

Here is the browse button code:

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);

        }

    }

Here is the xaml of the text and button controls.

        <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"/>

2 个答案:

答案 0 :(得分:1)

将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; }

在FileSelection ViewModel类中:

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;
        }
}

你可以填补空白......

答案 1 :(得分:0)

Add a ListBox control and when the user selects a file(s) add it to the ListBox.Items?