I am just trying to migrate some of my old apps from Win-forms to WPF. In my old win-form app I can send an array to a ListBox with the code below.
In my new WPF app, this will not populate the ListBox. What is weird is it does not error out, it just doesn’t work.
…well it did error the very first time I tested the app I got a message that said I didn’t have permissions, but now it just displays the message saying “done” without doing anything.
private void btnFolderBrowser_Click(object sender, RoutedEventArgs e)
{
getFileStructure();
System.Windows.Forms.MessageBox.Show("done!");
}
private void getFileStructure()
{
string myDir = "";
int i;
string filter = txtFilter.Text;
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.RootFolder = Environment.SpecialFolder.Desktop;
fbd.ShowNewFolderButton = false;
fbd.Description = "Browse to the root directory where the files are stored.";
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
myDir = fbd.SelectedPath;
try
{
txtRootDir.Text = fbd.SelectedPath;
string[] fileName = Directory.GetFiles(myDir, filter, SearchOption.AllDirectories);
for (i = 0; i < fileName.Length; i++)
lstFileNames.Items.Add(fileName[i]);
}
catch (System.Exception excep)
{
System.Windows.Forms.MessageBox.Show(excep.Message);
return;
}
}
In WPF you should be populating Properties in the code behind and binding you UI elements to those properties, its not good practice to reference/access UIElements from the code behind.
Here is a mockup example based on what I think you are trying to do.
Xaml:
Code:
Result: