【问题标题】:How do I make a ListView header to be clickable?如何使 ListView 标头可点击?
【发布时间】:2017-06-24 10:16:47
【问题描述】:

我有一个列表视图,其中包含一个应该在单击时响应的标题。

有没有办法创建一个可点击 ListView 标头?

【问题讨论】:

  • 你能给我们看一张截图和你的列表视图代码吗?谢谢!

标签: xaml listview uwp windows-10-universal uwp-xaml


【解决方案1】:

将例如 Button 作为标题放在 ListView 中没有问题:

<ListView Width="100" Margin="20">
    <ListView.Header>
        <Button Content="Clickable header" Click="Button_Click"/>
    </ListView.Header>
    <x:String>Sample element</x:String>
    <x:String>Sample element</x:String>
    <x:String>Sample element</x:String>
</ListView>

当然,您可以更改按钮的样式以满足您的需求。此外,您还可以更改标题,而不是更改 HeaderTemplate

<ListView Width="100" Margin="20" Header="Clickable header">
    <ListView.HeaderTemplate>
        <DataTemplate>
            <Button Content="{Binding}" Click="Button_Click"/>
        </DataTemplate>
    </ListView.HeaderTemplate>
    <x:String>Sample element</x:String>
    <x:String>Sample element</x:String>
    <x:String>Sample element</x:String>
</ListView>

【讨论】:

  • @mbob 也是一个有效的解决方案。请注意,还有其他方法,例如编辑 Style,尽管所有最简单的方法都依赖于 Header*/*HeaderTemplate 修改。
【解决方案2】:

我通过为 ListView.HeaderTemplate 添加一个行为解决了这个问题:

<Interactivity:Interaction.Behaviors>
    <Core:EventTriggerBehavior EventName="Tapped" SourceObject="{Binding ElementName=ListViewHeader}">
         <Core:InvokeCommandAction Command="{Binding DataContext.OnShowAllTicketsPressedCommand, ElementName=Project}"/>
    </Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 2016-02-18
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多