How to change the data source of Listbox to USB (external drive) or device instead of default relative path? RRS feed

  • Question

  • Hi everyone,

    I am still trying to follow some of the examples of binding data to a listbox. So far, I made it right using sample data from expression blend w/c uses the relative path (I dont know the right term..)


    How will I change the source path outside my application? i.e. flash drive,..bluetooth or any external devices.. or accessing a static path on my hard drive?

    Scenario Testing:

    I honestly want to display/show first the images from any selected external device and show it on my listbox and let the user select the images he want ed to use on my application.

    • Edited by RafeeNat Friday, November 25, 2011 10:23 PM
    Thursday, November 24, 2011 6:05 PM

All replies

  • You will need to post some of the code you are using, along with what you have tried, and what isn't working, in order for someone to give you a helpful answer.
    Monday, November 28, 2011 4:46 PM
  • Honestly I dont know behind the codes generated by expression blend but still I was able to get the sample data bind to my listbox (easy part though).

    For now, I want to know how to GET THE DATA NOT using the relative path (default of EB). e.i. flash drive or any drive (I can set a static drive letter just for testing) outside my solution directory.

    Is that possible with Expression Blend without writing any code? I mean all I want is just changing the default to my custom PATH.




    Rafee Nat
    Tuesday, November 29, 2011 4:01 AM
  • Hey Rafee,

    It sounds like you are trying to read files in a directory and return some sort of list maybe.

    You could do that with DirectoryInfo.

    Here's a sample for reading music files in "My Music" folder:

    	Width="640" Height="480">
    	<Grid x:Name="LayoutRoot">
    		<TextBlock TextWrapping="Wrap" Text="Enter the full folder path below:" HorizontalAlignment="Left" VerticalAlignment="Top" Width="268" Margin="8,8,0,0"/>
    		<TextBlock TextWrapping="Wrap" Text="Example:  C:\Users\Christine\Music\CalMusic" HorizontalAlignment="Left" VerticalAlignment="Top" Width="268" Margin="16,27.96,0,0" FontStyle="Italic"/>
    		<TextBox x:Name="myPath" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Margin="16,72.96,0,0" Width="260"/>
    		<ListBox x:Name="myList" HorizontalAlignment="Right" Width="303" Margin="0,8,8,8" SelectionChanged="playMedia"/>
    		<Button x:Name="myButton" Content="Get My Files" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="201,124,0,0" Click="getMyFiles"/>
    		<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Now Playing:" VerticalAlignment="Bottom" Margin="8,0,0,112"/>
    		<TextBlock x:Name="nowPlaying" TextWrapping="Wrap" VerticalAlignment="Bottom" Margin="16,0,0,90" Height="22" HorizontalAlignment="Left" Width="293"/>
    		<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Selected Song:" VerticalAlignment="Bottom" Margin="8,0,0,64"/>
    		<TextBlock x:Name="selectedPath" TextWrapping="Wrap" Text="{Binding SelectedItem, ElementName=myList}" VerticalAlignment="Bottom" Margin="16,0,0,8" Height="52" HorizontalAlignment="Left" Width="293"/>
    		<MediaElement x:Name="myMedia" HorizontalAlignment="Left" Width="100" Margin="8,210.04,0,131.96" LoadedBehavior="Manual" Volume="1" Source="{Binding Text, ElementName=selectedPath}"/>

    And the Code Behind:

    using System;
    using System.Windows;
    using System.IO;
    namespace WpfApplication1
    	public partial class MainWindow : Window
    		public MainWindow()
    		private void getMyFiles(object sender, System.Windows.RoutedEventArgs e)
    			string myFolder = this.myPath.Text;
    				DirectoryInfo di = new DirectoryInfo(myFolder);
    				foreach (FileInfo fi in di.GetFiles())
    			catch (Exception ex)
    			if (this.myList.Items.Count > 0)
    				this.myList.SelectedIndex = 0;
    		private void playMedia(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    			int i = this.myPath.Text.Length;
    			string str = this.myList.SelectedItem.ToString();
    			string str2 = str.Substring(i + 1);
    			this.nowPlaying.Text = str2;

    Good Luck.



    Tuesday, November 29, 2011 4:53 AM