【问题标题】:Apply DataTemplate to one type only仅将 DataTemplate 应用于一种类型
【发布时间】: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&lt;Genre&gt;

<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


【解决方案1】:

不要像现在这样将模板应用于列表框中的所有项,而是将模板作为资源添加到ListBox.Resources(或其他合适的资源字典),并带有隐式键(即由TargetType 隐含),并让模板为项目类型自动选择:

<ListBox
        x:Name="lstBox"
        Background="CadetBlue"
        FontFamily="Consolas"
        FontSize="14"
        FontWeight="DemiBold"
        ItemContainerStyle="{DynamicResource _ListBoxItemStyle}"
        MouseDoubleClick="lstBox_MouseDoubleClick">
    <ListBox.Resources>
        <DataTemplate 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>
    </ListBox.Resources>
</ListBox>

您可以根据需要为列表框中可能出现的其他数据类型添加其他模板。

【讨论】:

    猜你喜欢
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多