Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The easiest way to do this in WPF is with Databinding. You can simply bind an ObservableCollection to a DataGrid and the rest just works.</p> <p>To start with, you should create your own class to wrap the file name with a boolean field to indicate if it is selected or not.</p> <pre class="lang-cs prettyprint-override"><code>class FileSelection { public bool IsSelected { get; set; } public string FilePath { get; set; } public string FileName { get; set; } public string Picture { get; set; } } </code></pre> <p>Add a DataGrid to your form like so:</p> <pre class="lang-xml prettyprint-override"><code> &lt;DataGrid AutoGenerateColumns="False" Name="dataGrid1" BeginningEdit="dataGrid1_BeginningEdit"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridCheckBoxColumn Binding="{Binding IsSelected}"/&gt; &lt;DataGridTemplateColumn&gt; &lt;DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;Image Width="25" Height="25" Source="{Binding Path=Picture}" /&gt; &lt;/DataTemplate&gt; &lt;/DataGridTemplateColumn.CellTemplate&gt; &lt;/DataGridTemplateColumn&gt; &lt;DataGridTextColumn Binding="{Binding FileName}"/&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; </code></pre> <p>Then add code to your form to do the binding:</p> <pre class="lang-cs prettyprint-override"><code>public partial class MainWindow : Window { ObservableCollection&lt;FileSelection&gt; files; public MainWindow() { InitializeComponent(); this.files = new ObservableCollection&lt;FileSelection&gt;(); this.dataGrid1.ItemsSource = this.files; } </code></pre> <p>When you open your file dialog, simply add the returned path to the collection like so:</p> <pre class="lang-cs prettyprint-override"><code> var ofd = new OpenFileDialog(); var result = ofd.ShowDialog(); if (result == true) { var fs = new FileSelection { FilePath = ofd.FileName, FileName = System.IO.Path.GetFileNameWithoutExtension(ofd.FileName), Picture = @"[PATH TO IMAGE]", IsSelected = true }; this.files.Add(fs); } </code></pre> <p>I tested this by opening a messageBox window to show the currently selected files like so:</p> <pre class="lang-cs prettyprint-override"><code>var message = string.Join(Environment.NewLine, this.files.Where(f =&gt; f.IsSelected).Select(f =&gt; f.FilePath).ToArray()); MessageBox.Show(message); </code></pre> <p>Hope that does the trick!</p> <p><strong>UPDATE</strong></p> <p>I added another column to display an image using a DataGridTemplateColumn and changed the displayed text to only show the file name while retaining the full path in the data-bound list.</p> <p>I also added an event handler to the DataGrid so that you can double-click a row to manipulate the selected file. You will need to add the following method to your code-behind class to use it.</p> <pre class="lang-cs prettyprint-override"><code>private void dataGrid1_BeginningEdit(object sender, DataGridBeginningEditEventArgs e) { var file = e.Row.DataContext as FileSelection; // Delete the file here... e.Cancel = true; } </code></pre> <p>Note that the FileSelection bound to the clicked row is stored in the DataContext property of the Row. You need to set the Cancel property on the eventArgs parameter to true so that the row doesn't go into edit mode.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload