ListBox ItemTemplateSelector 不起作用

发布于 2024-12-25 19:15:35 字数 2843 浏览 1 评论 0原文

我正在尝试在 WPF ListBox 上使用 ItemTemplateSelector 并在网上查看了几个示例。看起来很简单,但我无法让它发挥作用。我希望有人能告诉我哪里出了问题:

首先,我有一个 DataTemplateSelector 类,定义如下:

Public Class DocketDataTemplateSelector
    Inherits DataTemplateSelector

    Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As DependencyObject) As DataTemplate
        Return DataDocketHeaderTemplate
    End Function

    Private _DataDocketHeaderTemplate As DataTemplate
    Public Property DataDocketHeaderTemplate() As DataTemplate
        Get
            Return _DataDocketHeaderTemplate
        End Get
        Set(ByVal value As DataTemplate)
            _DataDocketHeaderTemplate = value
        End Set
    End Property

    Private _DataDocketDataTemplate As DataTemplate
    Public Property DataDocketDataTemplate() As DataTemplate
        Get
            Return _DataDocketDataTemplate
        End Get
        Set(ByVal value As DataTemplate)
            _DataDocketDataTemplate = value
        End Set
    End Property
End Class

非常简单 - 只是暂时返回 DataDocketHeaderTemplate 数据模板,直到我可以让它工作。

然后,我的用户控件的资源定义如下:

<UserControl.Resources>
    <DataTemplate x:Key="docketHeaderTemplate">
        <TextBlock Text="Header Row Test" Background="Yellow"/>
    </DataTemplate>

    <DataTemplate x:Key="docketDataTemplate">
        <TextBlock Text="Data Row Test" Background="Green"/>
    </DataTemplate>

    <local:DocketDataTemplateSelector DataDocketHeaderTemplate="{StaticResource docketHeaderTemplate}" DataDocketDataTemplate="{StaticResource docketDataTemplate}" x:Key="myDataTemplateSelector"/>
</UserControl.Resources>

用户控件中的 ListBox 的定义如下:

    <ListBox ItemsSource="{Binding TestData}" ItemTemplateSelector="{StaticResource myDataTemplateSelector}"/>

最后,我的 TestData 列表在我的绑定视图模型中定义如下:

Private _listTestData As ObservableCollection(Of String) = Nothing
    Public Property TestData As ObservableCollection(Of String)
        Get
            If _listTestData Is Nothing Then
                _listTestData = New ObservableCollection(Of String)
                _listTestData.Add("Row 1")
                _listTestData.Add("Row 2")
                _listTestData.Add("Row 3")
            End If
            Return _listTestData
        End Get
        Set(ByVal value As ObservableCollection(Of String))
            _listTestData = value
            NotifyPropertyChanged("TestData")
        End Set
    End Property

现在,我希望我会看到一个列表我的列表框中的 3 行都显示“标题行测试”(因为我的 datatemplateselector 始终返回 DataDocketHeaderTemplate)。但我看到的是我的核心数据 第 1 行 第 2 行 第 3 行

这似乎表明我的重写 datatemplateselector 没有被击中(事实上,如果我在 DocketDataTemplateSelector 中设置断点,我在任何时候都不会看到它被击中)。我这哪里出错了?

谢谢

I am attempting to use an ItemTemplateSelector on a WPF ListBox and have looked at several examples online. Seemed simple enough but I cannot get it to work. I am hoping that someone can tell me where I've gone wrong:

Fist, I have an DataTemplateSelector class defined as follows:

Public Class DocketDataTemplateSelector
    Inherits DataTemplateSelector

    Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As DependencyObject) As DataTemplate
        Return DataDocketHeaderTemplate
    End Function

    Private _DataDocketHeaderTemplate As DataTemplate
    Public Property DataDocketHeaderTemplate() As DataTemplate
        Get
            Return _DataDocketHeaderTemplate
        End Get
        Set(ByVal value As DataTemplate)
            _DataDocketHeaderTemplate = value
        End Set
    End Property

    Private _DataDocketDataTemplate As DataTemplate
    Public Property DataDocketDataTemplate() As DataTemplate
        Get
            Return _DataDocketDataTemplate
        End Get
        Set(ByVal value As DataTemplate)
            _DataDocketDataTemplate = value
        End Set
    End Property
End Class

Very simple - just returns the DataDocketHeaderTemplate datatemplate for the time being until I can get it to work.

I then have my user control with the following as its resource definition:

<UserControl.Resources>
    <DataTemplate x:Key="docketHeaderTemplate">
        <TextBlock Text="Header Row Test" Background="Yellow"/>
    </DataTemplate>

    <DataTemplate x:Key="docketDataTemplate">
        <TextBlock Text="Data Row Test" Background="Green"/>
    </DataTemplate>

    <local:DocketDataTemplateSelector DataDocketHeaderTemplate="{StaticResource docketHeaderTemplate}" DataDocketDataTemplate="{StaticResource docketDataTemplate}" x:Key="myDataTemplateSelector"/>
</UserControl.Resources>

The ListBox in the user control is simply defined like this:

    <ListBox ItemsSource="{Binding TestData}" ItemTemplateSelector="{StaticResource myDataTemplateSelector}"/>

Then finally, my TestData list is defined in my bound viewmodel like so:

Private _listTestData As ObservableCollection(Of String) = Nothing
    Public Property TestData As ObservableCollection(Of String)
        Get
            If _listTestData Is Nothing Then
                _listTestData = New ObservableCollection(Of String)
                _listTestData.Add("Row 1")
                _listTestData.Add("Row 2")
                _listTestData.Add("Row 3")
            End If
            Return _listTestData
        End Get
        Set(ByVal value As ObservableCollection(Of String))
            _listTestData = value
            NotifyPropertyChanged("TestData")
        End Set
    End Property

Now, I expect I would see a list of 3 rows in my listbox all saying 'Header Row Test' (since my datatemplateselector is always returning DataDocketHeaderTemplate). But instead I see my core data of
Row 1
Row 2
Row 3

This seems to indicate that my overriding datatemplateselector is not being hit (indeed if I set a breakpoint in DocketDataTemplateSelector, at no time do I see it being hit). Where am I going wrong with this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的位置 2025-01-01 19:15:35

抱歉,我无法将此作为评论发布,我还没有获得足够的分数。

我刚刚尝试了你的示例代码(我的第一个 VB 项目),你猜怎么着,它按预期工作:黄色背景上的三次“标题行测试”。我已将 ListBox 放入 UserControl 的网格中,然后将 UserControl 放入 Window 的网格中,然后使用 TestData 属性将 UserControl 的 DataContext 设置为 ViewModel 对象。

您的示例代码无法证明某些问题一定是错误的,也许您可​​以提供更多信息。

Sorry i can not post this as comment, i haven't got enough score.

I just tried your example code (my first VB project) and guess what, it works as expected: three times "Header Row Test" on yellow background. I've put the ListBox in a Grid in the UserControl, then put the UserControl in a Grid in a Window, then set the DataContext of the UserControl to a ViewModel object with your TestData property.

Something must be wrong that is not demonstrated by your example code, maybe you can provide more info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文