【问题标题】:WPF Listbox Grouping Header Not BindingWPF列表框分组标题未绑定
【发布时间】:2015-12-29 05:37:18
【问题描述】:

我有一个 CollectionViewSource,它按 ObservableCollection 中的 Created 属性分组。分组在列表框中工作,但我无法获取标题文本以显示创建日期。

CollectionViewSource 如下:

<Window.Resources>
    <CollectionViewSource x:Key="TaskListColSource" Source="{Binding Path=TaskItems}">
        <CollectionViewSource.SortDescriptions>
            <componentModel:SortDescription PropertyName="Created" />
        </CollectionViewSource.SortDescriptions>

        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Created" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

</Window.Resources>

我的列表框组样式如下:

        <ListBox.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Foreground" Value="White" />
                        <Setter Property="Focusable" Value="False"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander IsExpanded="True" Foreground="White">
                                        <Expander.HeaderTemplate>
                                            <DataTemplate DataType="{x:Type models:TaskItem}">
                                                <TextBlock  x:Name="asdf" Text="{Binding Created}" Foreground="White"/>
                                            </DataTemplate>
                                        </Expander.HeaderTemplate>
                                        <ItemsPresenter />
                                    </Expander>

                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListBox.GroupStyle>

谁能帮我在相关的文本块中显示创建日期?

我通过以下方式绑定列表框:

<ListBox x:Name="TaskListBox" ItemsSource="{Binding Source={StaticResource TaskListColSource}}">

【问题讨论】:

    标签: wpf xaml listbox


    【解决方案1】:

    您需要像这样为您的扩展器设置Header

    <Expander IsExpanded="True" Foreground="White" 
              Header="{Binding}">
    </Expander>
    

    每个GroupItem 中隐含的DataContext 是每个数据项。所以Header应该被设置为每个数据项(通过{Binding})。 HeaderTemplate 具有隐含的 DataContext 作为其 Header,但您尚未为其分配任何内容。

    编辑

    如果你想要一些StringFormat,为什么不把它设置在TextBlock里面(让Header不变):

     <TextBlock  x:Name="asdf" Text="{Binding Created, StringFormat=dd MMM yy, ConverterCulture=en-GB}" Foreground="White"/>
    

    【讨论】:

    • 感谢它的工作,但是 Created 是一个 DateTime。它以美国格式显示,我正在尝试将其转换为更英国的格式。我知道这有点偏离滑雪道,但你知道我怎么能做到这一点吗?
    • @ChrisThomas 看到我的编辑,你应该在TextBlock 上这样做。 Header 应保持不变。
    • 我在哪里使用 TextBlock,我试图在 HeaderTemplate 中设置它,但它需要我设置一个 DataTemplate,如果我使用与我最初发布的结构类似的结构,我会返回到空白标题。
    • @ChrisThomas TextBlock 已经是你的了(你没看到它的名字asdf吗?)。它在HeaderTemplateDataTemplate 内。
    • 嗨,我设法通过使用 Text="{Binding Name, StringFormat=dd MMM yy, ConverterCulture=en-GB}" 让它工作,非常感谢您的帮助,我不要认为没有你的帮助我会接近整理它!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    相关资源
    最近更新 更多