【问题标题】:ListViewItem is not selected when I clicked on the text area单击文本区域时未选择 ListViewItem
【发布时间】:2015-07-09 07:52:02
【问题描述】:

我正在使用 ListView 使用数据模板显示人员姓名列表:

<Windows.Resources>
  <DataTemplate Datatype="{x:Type local:Person}">
   <ListViewItem Content="{Binding Path=Name}">
  </DataTemplates>
</Windows.Resources>
<ListView Name="myList" itemSource="{Binding}">

后面的代码是

ObservableCollection<Person> lst = SomeMethod();
myList.DataContext = lst;

列表视图显示人员的姓名,但是当我完全单击姓名时,什么也没发生(该项目没有聚焦并且 SelectionChanged 事件不起作用)但是如果我单击列表视图项目中名称的右侧部分它可以工作

我认为已连接到数据模板,因为当我删除模板并覆盖人员的 ToString 方法以返回 this.Name 时,它​​工作正常。

有什么想法吗?

谢谢。

【问题讨论】:

  • 尝试在模板中的 ListViewItem 周围放置边框。也许这有帮助?

标签: wpf listview datatemplate listviewitem


【解决方案1】:

你不应该在 DataTemplate 中使用 ListViewItem,它是一个 ContentControl,它通常描述内容应该如何显示在 inside ContentControls 中,而不是相反:P

您的 DataTemplate 正在生成这个(简化的)可视化树:

ListView
    ItemsPresenter
        ListViewItem
            ContentPresenter
                ListViewItem

额外的 ListViewItem 可能正在吞噬 Click 事件。相反,使用 TextBlock 之类的东西来显示文本,并且常规的 ListViewItem 行为应该按预期工作。

<DataTemplate Datatype="{x:Type local:Person}">
    <TextBlock Text="{Binding Path=Name}">
</DataTemplates>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-09
    • 2012-05-11
    • 2019-12-01
    • 2015-08-04
    • 1970-01-01
    • 2012-03-13
    • 2015-11-25
    • 2016-08-09
    相关资源
    最近更新 更多