Xamarin表单ListView绑定

why*_*ent 9 c# xaml listview xamarin.forms

现在我正在尝试让ListView拥有一些可绑定的CustomCells.我在XAML中将单元格定义为ListView.ItemTemplate下的DataTemplate下的ViewCell.

让我们简单地说,我有两个我在Cell中代表的字符串.ViewCell看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="simpletest.MyPage2">
    <ContentPage.Content>
        <StackLayout Spacing="10" x:Name="layout" Padding="8,10">
            <Label Text="{Binding Something}" />
            <ListView x:Name="listView">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Orientation="Horizontal">
                                <Label Text="{Binding Name}" />
                                <Label Text="{Binding Price}" />
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

我真的很想在XAML中保留它.我不喜欢通过在C#中编写整个内容来将其变成一个膨胀的世界.

我有一个ViewModel用于此页面,我在代码隐藏文件中设置绑定上下文.我的问题如下,我是否定义了一个在C#中扩展ViewCell以便绑定数据的类?如前所述,我不想在C#中定义布局.我想以这样的方式绑定.我想避免引用字段,因为文件背后的代码倾向于引用组件.这可能吗?我检查了Xamarin网站,该示例使用后面的代码混合和XAML(如果我正确解释它...).

编辑1:

我尝试编写一个模仿一些例子的文件,但这还不够.没有一段代码解释这个文件是代表XAML中的ViewCell的文件,并且没有明显的方法来填充列表而不访问代码隐藏中的列表字段.如果我只想使用绑定将一些项添加到此列表中,我会错过什么?

public class ReportedAssignmentCell : ViewCell
{
    public ReportedAssignmentCell()
    {
    }

    public static readonly BindableProperty NameProperty = BindableProperty.Create("Name", typeof(string), typeof(ReportedAssignmentCell), "");
    public static readonly BindableProperty PriceProperty = BindableProperty.Create("Price", typeof(string), typeof(ReportedAssignmentCell), "");



    public string Name
    {
        get { return (string)GetValue(NameProperty); }
        set { SetValue(NameProperty, value); }
    }
    public string Price
    {
        get { return (string)GetValue(PriceProperty); }
        set { SetValue(NameProperty, value); }
    }
}
Run Code Online (Sandbox Code Playgroud)

hva*_*an3 7

您需要将ListViews ItemsSource(非ItemSource)属性设置为项目列表.该项列表应包含在您的视图中.像这样的东西:

<ListView ItemsSource="{Binding ListOfItems}" ...>
...
</ListView>
Run Code Online (Sandbox Code Playgroud)

然后在你的ViewModel中:

private ObservableCollection<ItemModel> _listOfItems;
public ObservableCollection <ItemModel> ListOfItems {
    get { return _listOfItems ?? _listOfItems == new ObservableCollection<ItemModel>; }
    set {
        if(_listOfItems != value) {
            _listOfItems = value;
            SetPropertyChanged();
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在,如果你想使用自定义ReportedAssignmentCell你可以,但你也可以见好就收StackLayout,并Labels在那里.尝试设置ItemsSource我上面描述的类似,使用ListView您发布的当前XAML,看看是否能为您提供所需的效果.如果没有,请告诉我.

A ViewCell意味着保留其他项目,例如StackLayouts和Labels.但它不必包含那些Label将要绑定到的所有属性.Binding从ListView.ItemsSource你的传递到你的ListView.ItemTemplate.该绑定不需要ViewCell具有任何特殊属性来使绑定起作用.我希望这是有道理的.如果没有让我知道什么是不正确的.