在Search_Click中lstBox的来源是List<string>
在btnStats_Click lstBox的来源il List<Genre>
在我将DisplayMemberPath =“Name”添加为listBox属性后,Search_Click不会输出任何内容。
是否只能在btnStats_Click上将DisplayMemberPath =“Name”应用于lstBox?
public class Genre
{
public string Name { get; set; }
public int Count { get; set; }
public double Size { get; set; }
public string Drive { get; set; }
}
private void Search_Click(object sender, RoutedEventArgs e)
{
var path = Constants.allMoviesPath;
var ext = new List<string> { @".txt", @".ini", @".exe", @".mob", @".srt", @".ass" };
lstBox.ItemsSource = Directory.GetFiles(path, "*" + SearchString + "*", SearchOption.AllDirectories)
.Where(f => !ext.Contains(System.IO.Path.GetExtension(f)))
.Select(f => System.IO.Path.GetFileNameWithoutExtension(f))
.ToList();
}
private void btnStats_Click(object sender, RoutedEventArgs e)
{
lstBox.ItemsSource = FileLists.MoviesCountSizeStats();
}
MoviesCountSizeStats()的返回类型为List<Genre>
<ListBox
x:Name="lstBox"
Background="CadetBlue"
FontFamily="Consolas"
DisplayMemberPath="Name"
FontSize="14"
FontWeight="DemiBold"
ItemContainerStyle="{DynamicResource _ListBoxItemStyle}"
MouseDoubleClick="lstBox_MouseDoubleClick" />
答案 0 :(得分:1)
这很容易。如果您想更改lstBox.DisplayMemberPath
...只需更改它。
private void Search_Click(object sender, RoutedEventArgs e)
{
var path = Constants.allMoviesPath;
var ext = new List<string> { @".txt", @".ini", @".exe", @".mob", @".srt", @".ass" };
lstBox.ItemsSource = Directory.GetFiles(path, "*" + SearchString + "*", SearchOption.AllDirectories)
.Where(f => !ext.Contains(System.IO.Path.GetExtension(f)))
.Select(f => System.IO.Path.GetFileNameWithoutExtension(f))
.ToList();
// Null when it should be null
lstBox.DisplayMemberPath = null;
}
private void btnStats_Click(object sender, RoutedEventArgs e)
{
lstBox.ItemsSource = FileLists.MoviesCountSizeStats();
// "Name" when it should be "Name"
lstBox.DisplayMemberPath = "Name";
}