【问题标题】:ListView Container in UWPUWP 中的 ListView 容器
【发布时间】:2016-10-28 17:37:31
【问题描述】:

我正在尝试获取当前选定的 ListView 项的容器。 ContainerFromItem 总是返回 null 并且编译器抱怨这个函数可能已经过时了。

有什么想法吗?

【问题讨论】:

标签: c# listview uwp


【解决方案1】:

ContainerFromItem 总是返回 null。

ItemsControl.ContainerFromItem 方法可以获取指定项的容器。例如,我们可以从ListView 中的选定项中获取ListViewItem 容器。如果您得到 null,则可能该项目不存在容器或您的代码有问题。

我正在尝试获取当前选定的 ListView 项的容器。

这是一个完整的演示,用于获取当前选择的ListViewItem 的容器。 XAML 代码:

<ListView
    Name="CListView"
    Margin="10"
    HorizontalAlignment="Center"         
    ItemsSource="{x:Bind categories}"
    SelectionChanged="CListView_SelectionChanged">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="local:Category">
            <StackPanel             
                Background="{x:Bind backgroundcolor}"
                Orientation="Horizontal">
                <TextBlock                           
                    FontSize="17"
                    FontWeight="Bold"
                    Text="{x:Bind Name}" />
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

后面的代码

  ObservableCollection<Category> categories = new ObservableCollection<Category> { };
  public ListViewContainer()
  {
      this.InitializeComponent();
      categories = new ObservableCollection<Category>
      {
          new Category {Name="name1",details="color1" ,backgroundcolor="#D90015"},
          new Category {Name="name2",details="color2" ,backgroundcolor="#DC1C17"},
          new Category {Name="name3",details="cplor3",backgroundcolor="#DE3A17" },
          new Category {Name="name3",details="color4",backgroundcolor="#E25819" }
      };
  } 
  private void CListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
      var  container = CListView.ContainerFromItem(CListView.SelectedItem);
      ListViewItem item = container as ListViewItem;
      System.Diagnostics.Debug.WriteLine(item.ActualHeight);
  }

【讨论】:

  • 效果很好。谢谢。
猜你喜欢
  • 2020-01-06
  • 2017-01-24
  • 2016-11-25
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
  • 1970-01-01
  • 2018-02-06
相关资源
最近更新 更多