![](/img/trans.png)
[英]I would like to know that whether or not data binded to the repeater in item template
[英]Listbox Not Displaying Data (Is Binded With Item Template)
我知道這基本上是一堆重復的詢問,但是我遇到了困難,我的組合框列出了硬盤,單擊時它們應該將圖像填充到列表框中,這要歸功於之前的幫助,它可以正常運行,只是列表框什么都不顯示,我將其綁定在項目模板等中,因此我卡住了為什么不顯示任何內容
/// <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>
我認為您應該將ItemsSource
綁定到組合框的SelectedItem
,並提供一些Converter
以將所選項目轉換為相應的List<Photo>
。 否則,當組合框的SelectedItem
更改時,您必須通知LoadImages
屬性更改。 嘗試以下代碼(在過程代碼和XAML代碼中都進行了修改):
后面的代碼 :
public class SelectedItemToLoadImagesConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture){
List<Photos> images = new List<Photos>();
if (value != null) //If a item has been selected
{
foreach (string filename in System.IO.Directory.GetFiles(value.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 object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){
throw new NotImplementedException();
}
}
XAML代碼 :
<Window.Resources>
//...
<local:SelectedItemToLoadImagesConverter x:Key="imagesConverter"/>
</Window.Resources>
<DockPanel LastChildFill="False">
<ListBox
Name="ImageListBox"
DockPanel.Dock = "Right"
ItemsSource = "{Binding ElementName=HDDSelectionBox,Path=SelectedItem,Converter={StaticResource imagesConverter}}"
ItemTemplate="{StaticResource MyImageTemplate}"
Width="200"/>
<ComboBox Width="80" Height="50" DockPanel.Dock="Top" Name="HDDSelectionBox" Loaded="HDDSelectionBox_Loaded" ></ComboBox>
</DockPanel>
</Grid>
請注意,您可以在代碼中刪除LoadImages
屬性。 還請注意,我使用local
前綴作為對您的項目名稱空間的引用,在該項目中定義了類SelectedItemToLoadImagesConverter
,希望您知道如何在XAML代碼中定義xmlns
。
好吧,我先問一個簡單的問題:如果我們假設您只有C:驅動器,C:\\文件夾是否包含任何圖像?
如果是,那么我們討論下一個問題,可能在代碼中。
如果沒有,那么顯然您什么也沒顯示。 但是,如果子文件夾中有圖像,則可能需要更改對API GetFiles
的使用。 正確的應該從所有子文件夾中獲取文件:
foreach (string filename in System.IO.Directory.GetFiles(
value.ToString(),
"*.png", //assuming that you want PNG images only
System.IO.SearchOption.AllDirectories))
有關GetFiles的更多信息,請參見此處 。
如果這解決了您的問題,則您可能需要增強它以使用圖像枚舉,因為在整個驅動器中搜索文件可能會花費一些時間,在此期間您的應用將無響應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.