c# - 如何在 xaml 中创建 ListView 项

标签 c# xaml listview

正在阅读官方Xamarin documentation about listview ,您在 xaml(xml 文件)中创建模板并在代码中以编程方式填充 listView 项目:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:constants="clr-
 namespace:XamarinFormsSample;assembly=XamarinFormsXamlSample"
 x:Class="XamarinFormsXamlSample.Views.EmployeeListPage"
 Title="Employee List">
   <ListView x:Name="EmployeeView">
     <ListView.ItemTemplate>
       <DataTemplate>
         <TextCell Text="{Binding DisplayName}" />
       </DataTemplate>
     </ListView.ItemTemplate>
   </ListView>
 </ContentPage>

然后像这样用代码填充项目:

private ObservableCollection<Employee> employees = new ObservableCollection<Employee>();

public EmployeeListPage()
{
  // just for demonstration purposes
  employees.Add(new Employee{ DisplayName="Rob Finnerty"});
  employees.Add(new Employee{ DisplayName="Bill Wrestler"});

  //defined in XAML to follow
  EmployeeView.ItemsSource = employees;
  ...
}

有没有办法在 xaml 中定义项目?我正在设置一个简单的设置页面,并想在 xml 中添加项目。我最终使用了 TableView ,但还是很好奇是否可以做到?

最佳答案

您可以定义 ListView.Items 并在其中放置一些其他 WPF 控件。

<ListView>
    <ListView.Items>
        <Label Content="123"/>
        <TextBox Text="ABC"/>
        <CheckBox Checked="True"/>
    </ListView.Items>                    
</ListView>

关于c# - 如何在 xaml 中创建 ListView 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46110162/

相关文章:

android - 在 ListView 上显示静态数据

Android - 如何在从数据库中添加/删除项目后动态刷新 ListView

具有多个客户端的 C# 套接字(多线程方法)

c# - 在 C# 4.0 中获取对象的实例名称没有对象类型名称

c# - 如何从 Xamarin Forms 中的主从页面传递字符串数据

c# - 让 FocusManager 等到数据绑定(bind)完成?

c# - 无法序列化和反序列化UserControl

c# - 编辑 XAML 导致 Visual Studio 的设计器崩溃

c# - 具有 maxlength 属性的文本框的工具提示

listview - 控制Listview中元素的宽度 - flutter