Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9232681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:22:18+00:00 2026-06-18T06:22:18+00:00

I am just trying to migrate some of my old apps from Win-forms to

  • 0

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:22:19+00:00Added an answer on June 18, 2026 at 6:22 am

    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:

    <Window x:Class="WpfApplication14.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           Title="MainWindow" Height="350" Width="525" Name="UI">
        <StackPanel DataContext="{Binding ElementName=UI}">
            <TextBox Text="{Binding TextRootDir}" IsReadOnly="True" />
            <ListBox ItemsSource="{Binding MyFiles}" SelectedItem="{Binding SelectedFile}" Height="244" />
            <TextBox Text="{Binding TextFilter, UpdateSourceTrigger=PropertyChanged}" />
            <Button Content="Browse" Click="btnFolderBrowser_Click" >
                <Button.Style>
                    <Style TargetType="{x:Type Button}">
                        <Setter Property="IsEnabled" Value="True" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding TextFilter}" Value="">
                                <Setter Property="IsEnabled" Value="False" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
        </StackPanel>
    
    </Window>
    

    Code:

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private string _textRootDir;
        private string _textFilter = string.Empty;
        private string _selectedFile;
        private ObservableCollection<string> _myFiles = new ObservableCollection<string>();
    
        public MainWindow()
        {
            InitializeComponent();
        }
    
        public ObservableCollection<string> MyFiles
        {
            get { return _myFiles; }
            set { _myFiles = value; }
        }
    
        public string SelectedFile
        {
            get { return _selectedFile; }
            set { _selectedFile = value; NotifyPropertyChanged("SelectedFile"); }
        }
    
        public string TextFilter
        {
            get { return _textFilter; }
            set { _textFilter = value; NotifyPropertyChanged("TextFilter"); }
        }
    
        public string TextRootDir
        {
            get { return _textRootDir; }
            set { _textRootDir = value; NotifyPropertyChanged("TextRootDir"); }
        }
    
        private void btnFolderBrowser_Click(object sender, RoutedEventArgs e)
        {
            GetFileStructure();
            MessageBox.Show("done!");
        }
    
        private void GetFileStructure()
        {
            string myDir = "";
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.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
                {
                    TextRootDir = fbd.SelectedPath;
                    MyFiles.Clear();
                    foreach (var file in Directory.GetFiles(myDir, TextFilter, SearchOption.AllDirectories))
                    {
                        MyFiles.Add(file);
                    }
                }
                catch (Exception excep)
                {
                    MessageBox.Show(excep.Message);
                    return;
                }
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    Result:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are trying to migrate a legacy intranet ASP .NET web app from Forms
I'm trying to migrate from C2DM to GCM, and it mostly works just fine.
I am trying to migrate some code from using ElementTree to using lxml.etree and
This morning, while trying to migrate some files from my test server to my
While trying to migrate from Symfony 2.0 to 2.1, I've found some interesting issue.
I'm trying to migrate a large app from XslTransform to compiled xsl files and
Just trying to establish whether prototype can do something like $$('#ID a:last').css('color','#111'); Any ideas
Just trying to get my head around what can happen when things go wrong
Just trying to canvas some opinions here. I was wondering how people go about
I've inherited a project which we are trying to migrate to MySQL 5 from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.