I'm kinda new at both programming, and WPF. I know that this is pretty easy, but I nothing I've tried as worked so far..... I want to fill a Listbox with images from a folder. I also need to know how to force my listbox from allowing scrolling to the side. So far I haven't stumbled upon anything that seems to work.
Here is my C# code that adds the files in chosen folder to a List Basically I want the Listbox to be use to keep a history log of the pictures that the user has chosen as backgrounds.
IList<Bitmap> HistoryImages = new List<Bitmap>();
foreach(String imagefile in Directory.GetFiles( @"C:\ProgramData\etc" ))
{
HistoryImages.Add( new Bitmap( imagefile) );
}
Found something that worked for me! XAML Code:
<ListBox Name="ImageLog" Background="Transparent" IsEnabled="True"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
ItemsSource="{Binding Path=Image}" BorderThickness="0"
SelectionChanged="ImageLog_SelectionChanged_1">
</ListBox>
C# Code:
foreach(string myFile in Directory.GetFiles( @"C:\ProgramData\MyApp" ) )
{
System.Windows.Controls.Image myLocalImage = new System.Windows.Controls.Image(); ;
myLocalImage.Height = 200;
myLocalImage.Margin = new Thickness( 5 );
BitmapImage myImageSource = new BitmapImage();
myImageSource.BeginInit();
myImageSource.UriSource = new Uri( @"file:///" + myFile );
myImageSource.EndInit();
myLocalImage.Source = myImageSource;
filePath.Add( myFile );
ImageLog.Items.Add(myLocalImage);
}
This requires the basics of data binding and data templating . (If you read and understood all that you should be able to do it.)
About the scrolling, set ScrollViewer.HorizontalScrollBarVisibility
as attached property on the ListBox
to Disabled
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.