为什么Listview Itemtemplate比ViewBase快?

时间:2019-04-03 18:27:58

标签: c# wpf vb.net xaml listview

我有一个WPF ListView和两个绑定数据的方案,如下所示:

  1. 设置“ ItemTemplate”

    ItemTemplate =“ {DynamicResource MyCustomDataTemplate}”

  2. 设置“ ViewBase”

    ListView.View = MyCustomView

“ MyCustomView”如下所示:

<local:TileView x:Key="MyCustomView" ItemTemplate="{StaticResource MyCustomDataTemplate}"  CornerRadius="4" SelectionBrush="#FFF18C00" />

我使用“ ViewBase”是因为我需要选择画笔和其他一些额外数据,并且“ TileView”也继承自“ ViewBase”类。

现在的问题是为什么没有方案。 1比方案2快很多?这意味着对于4000个项目,第一种情况需要4秒来加载所有项目,而第二种情况则需要大约一分钟。

由于其附加功能,我需要使用第二种方案。

有什么帮助使其更快?


有关“ TileView”的更多信息:

Public Class TileView
Inherits ViewBase
Private _itemTemplate As DataTemplate
Private _SelectionBrush As Brush = Brushes.Transparent
Private _CornerRadius As New CornerRadius(0)

Public Shared ReadOnly SelectionBrushProerty As DependencyProperty = DependencyProperty.Register("SelectionBrush", GetType(Brush), GetType(TileView))
   Public Shared ReadOnly ItemContainerStyleProperty As DependencyProperty = ItemsControl.ItemContainerStyleProperty.AddOwner(GetType(TileView))
Public Property ItemContainerStyle() As Style
    Get
        Return CType(GetValue(ItemContainerStyleProperty), Style)
    End Get
    Set(ByVal value As Style)
        SetValue(ItemContainerStyleProperty, value)
    End Set
End Property
Public Property ItemTemplate() As DataTemplate
    Get
        Return _itemTemplate
    End Get
    Set(ByVal value As DataTemplate)
        _itemTemplate = value
    End Set
End Property
Public Property SelectionBrush() As Brush
    Get
        Return MyBase.GetValue(SelectionBrushProerty)
    End Get
    Set(ByVal value As Brush)
        MyBase.SetValue(SelectionBrushProerty, value)
    End Set
End Property

Protected Overrides ReadOnly Property DefaultStyleKey() As Object
    Get
        Return New ComponentResourceKey([GetType](), "TileView")
    End Get
End Property
Protected Overrides ReadOnly Property ItemContainerDefaultStyleKey() As Object
    Get
        Return New ComponentResourceKey([GetType](), "TileViewItem")
    End Get
End Property
  Public Property CornerRadius As CornerRadius
    Get
        Return _CornerRadius
    End Get
    Set(value As CornerRadius)
        _CornerRadius = value
    End Set
End Property  

EndClass

并且无需说明“ MyCustomDataTemplate”的结构,因为在两种情况下它是相同的。

谢谢大家。

0 个答案:

没有答案