【问题标题】:Cant bind property in ListView无法在 ListView 中绑定属性
【发布时间】:2020-09-20 17:43:41
【问题描述】:

我正在编写一个天气预报应用程序,但我无法在 ListView 中绑定对象的属性。

集合由具有字符串属性“Temperature”的 WeatherModel 对象组成

Xaml 看到集合并输出它,但找不到对象属性“Temperature”。

如何绑定“温度”属性?

这是我的 XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="IMAP.View.Weather"
             xmlns:local="clr-namespace:IMap.ViewModel;assembly=IMAP">

    <ContentPage.Resources>
        <local:ForecastWeatherViewModel x:Key="vm"/>
    </ContentPage.Resources>
    <ContentPage.Content>
        <ListView ItemsSource="{Binding items, Source={StaticResource vm}}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal" Padding="2,15">

                            <Label Text="{Binding Temperature}"/>

                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

这是我的视图模型

namespace IMap.ViewModel
{
    public class ForecastWeatherViewModel : ViewModel
    {
        public ObservableCollection<WeatherModel> WeatherList { get; set; }

        public ObservableCollection<WeatherModel> items
        {
            get
            {
                return WeatherList;
            }
        }



        public ForecastWeatherViewModel()
        {
            WeatherList = new ObservableCollection<WeatherModel>();

            WeatherList.Add(new WeatherModel()
                {
                    Temperature = SomeTemp
                });

            WeatherList.Add(new WeatherModel()
                {
                    Temperature = SomeTemp
                });

            OnPropertyChange();

        }
    }
}

这是我的模特

namespace IMAP.Model
{
    public class WeatherModel
    {
        public string Temperature;
    }
}

【问题讨论】:

    标签: c# visual-studio xaml xamarin xamarin.forms


    【解决方案1】:

    首先,您不能绑定到字段,而是为此目的使用属性。

    public class WeatherModel
    {
        public string Temperature { get; set; }
    }
    

    然后您可以修复视图模型中的集合管理。还具有以下命名规则:属性的大写字母和字段的小写字母。现在,同一个集合有两个公共属性,让我们创建一个。

    public class ForecastWeatherViewModel : ViewModel
    {
        // backing field, never interact with it outside of the property getter/setter
        private ObservableCollection<WeatherModel> items;
    
        // public property, bind here, interact with it
        public ObservableCollection<WeatherModel> Items
        {
            get => items; // same as "get { return items; }" but shorter
            set
            {
                items = value;
                OnPropertyChange();
            }
        }
    
        public ForecastWeatherViewModel()
        {
            Items = new ObservableCollection<WeatherModel>();
    
            Items.Add(new WeatherModel()
            {
                Temperature = SomeTemp
            });
    
            Items.Add(new WeatherModel()
            {
                Temperature = SomeTemp
            });
        }
    }
    
    <ListView ItemsSource="{Binding Items, Source={StaticResource vm}}">
    

    【讨论】:

      猜你喜欢
      • 2021-03-31
      • 2012-01-08
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      • 2018-08-21
      相关资源
      最近更新 更多