【发布时间】:2013-11-24 05:30:37
【问题描述】:
我知道这基本上是很多人重复问的问题,但是我遇到了困难,我的组合框列出了硬盘,单击时它们应该用图像填充列表框,这要归功于事先的帮助,除了列表框之外,它运行没有错误什么都不显示,我将它绑定在项目模板等中,所以我不知道为什么什么都不显示
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private void HDDSelectionBox_Loaded(object sender, RoutedEventArgs e) //ComboBox Lists Local Hardrives
{
string[] drives = Environment.GetLogicalDrives(); //Drive Letters, Into A String Array
foreach (string drive in drives)
{
HDDSelectionBox.Items.Add(drive); //Adds Each Drive Letter As A Combox Box Item
}
}
public List<Photos> LoadImages ///List Retrieves and Loads Photos
{
get
{
List<Photos> images = new List<Photos>();
if (HDDSelectionBox.SelectedItem != null) //If a item has been selected
{
foreach (string filename in System.IO.Directory.GetFiles(HDDSelectionBox.SelectedItem.ToString()))
{
try
{
images.Add( //Add To List
new Photos(
new BitmapImage(
new Uri(filename)),
System.IO.Path.GetFileNameWithoutExtension(filename)));
}
catch { ; } //Skips Any Image That Isn't Image/Cant Be Loaded
}
}
return images;
}
}
}
public class Photos
{
private ImageSource _image;
private string _name;
public Photos(ImageSource image, string name)
{
_image = image;
_name = name;
}
public override string ToString()
{
return _name;
}
public ImageSource Image
{
get { return _image; }
}
public string Name
{
get { return _name; }
}
} // END MyImage CLASS
}
xaml
<Window.Resources>
<DataTemplate x:Key="MyImageTemplate">
<StackPanel>
<Image
Source="{Binding Image}" Width ="100" Height="100" />
<TextBlock Text ="{Binding Name}" Width = "100" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<DockPanel LastChildFill="False">
<ListBox
Name="ImageListBox"
DockPanel.Dock = "Right"
ItemsSource = "{Binding LoadImages}"
ItemTemplate="{StaticResource MyImageTemplate}"
Width="200"/>
<ComboBox Width="80" Height="50" DockPanel.Dock="Top" Name="HDDSelectionBox" Loaded="HDDSelectionBox_Loaded" ></ComboBox>
</DockPanel>
</Grid>
【问题讨论】:
-
确定这叫
HDDSelectionBox_Loaded? -
它是,那部分是加载本地驱动器,它确实可以工作,它只是点击那些列出的驱动器并没有在列表框中列出任何东西
标签: c# wpf list binding listbox