C# 如何将类对象数据绑定到ViewCell?

C# 如何将类对象数据绑定到ViewCell?,c#,xamarin,C#,Xamarin,我需要将类对象数据绑定到自定义ViewCell。我使用{Binding propName}作为ViewCell内控件上的字段。我不确定是否正确添加了ViewCell,因为它显示为[AppName.GridView],并且没有显示我构建的控件。将此控件与自定义类绑定的正确方法是什么 <?xml version="1.0" encoding="UTF-8"?> <ViewCell xmlns="http://xamarin.com/schemas/2014/forms"

我需要将类对象数据绑定到自定义ViewCell。我使用
{Binding propName}
作为ViewCell内控件上的字段。我不确定是否正确添加了ViewCell,因为它显示为
[AppName.GridView]
,并且没有显示我构建的控件。将此控件与自定义类绑定的正确方法是什么

<?xml version="1.0" encoding="UTF-8"?>
  <ViewCell  xmlns="http://xamarin.com/schemas/2014/forms"
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
       Height="50">
   <Frame  HeightRequest="50" Padding="0" Margin="1" BorderColor="blue" CornerRadius="4" BackgroundColor="lightBlue">
    <Grid BindingContext="Binding local:di" >
        <Grid.RowDefinitions >
            <RowDefinition Height="34"/>
            <RowDefinition Height="40"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" BackgroundColor="Transparent" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="3"/>
                <ColumnDefinition Width="10*" />
                <ColumnDefinition Width="5*" />
                <ColumnDefinition Width="5*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions >
                <RowDefinition Height="40"></RowDefinition>
            </Grid.RowDefinitions>
            <Button x:Name="btnItem" Text="{Binding itemName}" Clicked="BtnClicked" Padding="0" Grid.Column="1" FontSize="20" />
            <Label x:Name="lbAmt" Text="{Binding amt}" Grid.Column="2" Margin="2" VerticalTextAlignment="Center"  HorizontalTextAlignment="End" FontSize="20" BackgroundColor="lightBlue" />
            <Label x:Name="lbType" Text="{Binding amtType}" Grid.Column="3" Margin="2" VerticalTextAlignment="Center" FontSize="20" BackgroundColor="lightBlue" />
        </Grid>
        ...
    </Grid>
</Frame>

...

用于添加对象的代码

        DItem di = new DItem() { itemName = "someName", amt = 1, amtType = xf };
        GridItems gi = new GridItems() { di = di };
        ObservableCollection<GridItems> lv = new ObservableCollection<GridItems>();
        lv.Add(gi);
        lvItems.ItemsSource = lv;
DItem di=new-DItem(){itemName=“someName”,amt=1,amtType=xf};
GridItems gi=newgriditems(){di=di};
ObservableCollection lv=新的ObservableCollection();
lv.Add(gi);
lvItems.ItemsSource=lv;

如果您想通过listview(MVVM)实现数据绑定, 下面是跑步截图。

首先,您可以创建一个模型
DItem
。我实现了
INotifyPropertyChanged
界面,当值发生变化时,布局也会发生变化

      public class DItem: INotifyPropertyChanged
{

    string _itemName;
    public string itemName
    {
        set
        {
            if (_itemName != value)
            {
                _itemName = value;
                OnPropertyChanged("itemName");

            }
        }
        get
        {
            return _itemName;
        }
    }

    int _amt;
    public int amt
    {
        set
        {
            if (_amt != value)
            {
                _amt = value;
                OnPropertyChanged("amt");

            }
        }
        get
        {
            return _amt;
        }
    }

    string _amtType;
    public string amtType
    {
        set
        {
            if (_amtType != value)
            {
                _amtType = value;
                OnPropertyChanged("amtType");

            }
        }
        get
        {
            return _amtType;
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
然后,实现ModelView。
DItemViewModel
,我们不需要创建
GridItems
,只需将
DItem
添加到
ObservableCollection

下面是listview的背景代码

      <ListView ItemsSource="{Binding lv}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Frame  HeightRequest="50" Padding="0" Margin="1" BorderColor="blue" CornerRadius="4" BackgroundColor="lightBlue">
                        <Grid >
                            <Grid.RowDefinitions >
                                <RowDefinition Height="34"/>
                                <RowDefinition Height="40"/>
                            </Grid.RowDefinitions>
                            <Grid Grid.Row="0" BackgroundColor="Transparent" >
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="3"/>
                                    <ColumnDefinition Width="10*" />
                                    <ColumnDefinition Width="5*" />
                                    <ColumnDefinition Width="5*" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions >
                                    <RowDefinition Height="40"></RowDefinition>
                                </Grid.RowDefinitions>
                                <Button x:Name="btnItem" Text="{Binding itemName}" Clicked="btnItem_Clicked" Padding="0" Grid.Column="1" FontSize="20" />
                                <Label x:Name="lbAmt" Text="{Binding amt}" Grid.Column="2" Margin="2" VerticalTextAlignment="Center"  HorizontalTextAlignment="End" FontSize="20" BackgroundColor="lightBlue" />
                                <Label x:Name="lbType" Text="{Binding amtType}" Grid.Column="3" Margin="2" VerticalTextAlignment="Center" FontSize="20" BackgroundColor="lightBlue" />
                            </Grid>

                        </Grid>
                    </Frame>

                </ViewCell>

            </DataTemplate>    

        </ListView.ItemTemplate>

    </ListView>
  public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        BindingContext = new DItemViewModel();
    }
    private void btnItem_Clicked(object sender, EventArgs e)
    {

    }
}
这里有一些关于Xamarin形式的MVVM的有用文章。


您不需要显式设置ViewCell的BindingContext,ListView将为您解决这一问题。您的绑定表达式应该类似于
Text=“{binding di.amt}”
Ok。我仍然在列表视图中看到
[AppName.GridItem]
,ideas?它是显示在其中一个标签中,还是显示整个单元格?如果是第二种情况,可能是模板设置有问题,您没有发布代码。整个callI没有使用模板。我应该调查一下。
  public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        BindingContext = new DItemViewModel();
    }
    private void btnItem_Clicked(object sender, EventArgs e)
    {

    }
}