【发布时间】:2017-05-05 21:23:09
【问题描述】:
我正在尝试仅将数据模板应用于键入Genre。但它也会在Search_Click 之后应用于List<string>。如何使ListBox 仅将DataTemplate 应用于Genre 数据?
<DataTemplate x:Key="genreTemplate" DataType="{x:Type local:Genre}">
<Border BorderBrush="Green" BorderThickness="2">
<StackPanel Margin="4">
<TextBlock
FontSize="20"
Foreground="Red"
Text="{Binding Name}"
TextAlignment="Center" />
<TextBlock
FontSize="16"
Text="{Binding Count}"
TextAlignment="Right" />
</StackPanel>
</Border>
</DataTemplate>
代码隐藏:
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"
FontSize="14"
FontWeight="DemiBold"
ItemContainerStyle="{DynamicResource _ListBoxItemStyle}"
ItemTemplate="{StaticResource genreTemplate}"
MouseDoubleClick="lstBox_MouseDoubleClick" />
【问题讨论】:
-
我认为从
中删除 ItemTemplate 属性并将模板定义放入 应该可以解决问题
标签: c# wpf data-binding listbox