Search code examples
c#.netwpfdesktop-applicationapp.xaml

Is there a way to bind a combobox with a Directory at the local disc C:/ or D:/


I'm Using WPF in Visual Studio 2017 and I am wandering if there is a way to bind a file source ("C:/example", "*.xml") to a Combobox programmatically.

the idea is to have all the xml files in a Combobox or a List, withoud being obliged to load each time a specific xml file with xDoc.load(path) a 100 times

I tried to get this files into my Combobox with SelectionChanged Event but it didn't work.

Any idea about how to get my files.

xaml Event declaration

<ComboBox x:Name="SourceFile" SelectionChanged="ComboBox_Source"/>

My Essai

private void ComboBox_Source(object sender, SelectionChangedEventArgs e)
{
    DirectoryInfo d = new DirectoryInfo(@"c:\export");
    FileInfo[] Files = d.GetFiles("*.xml");
    foreach(FileInfo file in Files)
    {
        this.SourceFile.Items.Add(file);

    }

Solution

  • I couldn't add the code but I found a way to my Question. I used Directory Info to get to the folder in my Local disc then with the FileInfo class of System.IO I get all the files with the extention that I want and Eventually add this files as Items in my CheckBox

    MySolution