微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从另一个ListView绑定ListView中的对象属性

如何解决如何从另一个ListView绑定ListView中的对象属性

我正在用MVVM编写xamarin天气预报应用程序。

我填写了$(document).ready(function(){ if (window.location.pathname != "" || window.location.pathname.indexOf("index")>0) { $('#main-nav').css('background-color','black'); } }); ObservableCollection<DailyWeather>

viewmodel中

ObservableCollection<HourlyWeather>

模型

private ObservableCollection<DailyWeather> dailyWeather;

        public ObservableCollection<DailyWeather> DailyWeather
        {
            get => dailyWeather;
            set
            {
                dailyWeather = value;
                OnPropertyChange();
            }
        }
public class DailyWeather
    {
        public int DayOfYear { get; set; }
        public ObservableCollection<HourlyWeather> HourlyWeather { get; set; }

    }

Xaml代码

public class HourlyWeather
    {
        public string Temperature { get; set; }
        public string Time { get; set; }
    }

父级ListView输出集合和“ DayOfYear”。

子级ListView输出输出集合,并看到对象属性“温度”和“时间”,但不输出它们,为什么?

所有收藏集都已填满。

为什么要从ListViews删除ViewCell,然后应用程序出现“指定的转换无效”异常?

解决方法

您需要从绑定中删除此 Source = {StaticResource vm}

将此添加到您的XAML:

 <ContentPage.BindingContext>
    <vm:VIEWMODEL></vm:VIEWMODEL>
</ContentPage.BindingContext>

并将父级列表视图更改为此:

<ListView ItemsSource="{Binding DailyWeather}" RowHeight="200">

enter image description here

,

删除ListView的ItemSource中的A.belongsTo(B); return await A.findAll({ attributes: [ [Sequelize.col('b.id'),'Id'],[Sequelize.col('b.name'),'Name'] ],raw: true,where: { /*Some condition*/ },include: { model: B,attributes: [],required: true },}); 。并在页面后面的代码中设置以下代码。

Source={StaticResource vm}

整个代码:

   this.BindingContext = this;

截屏:

enter code here

,

如果使用mvvm,则应使用Locator。这是工作示例。更改了一些标签,因为pc上现在没有WPF。

YourVM.cs

class YourVM : INotifyPropertyChanged
    {
        private ObservableCollection<DailyWeather> dailyWeather;

        public ObservableCollection<DailyWeather> DailyWeather
        {
            get => dailyWeather;
            set
            {
                dailyWeather = value;
                OnPropertyChange(nameof(DailyWeather));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChange(string propertyName)
        {
            PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
        }

        public YourVM()
        {
            DailyWeather = new ObservableCollection<DailyWeather>()
            {
                new DailyWeather()
                {
                     DayOfYear=2011,HourlyWeather=new ObservableCollection<HourlyWeather>()
                      {
                          new HourlyWeather()
                          {
                               Temperature="1",Time="2011-01-02"
                          },new HourlyWeather()
                          {
                                Temperature="2",Time="2011-01-03"
                          },new HourlyWeather()
                          {
                               Temperature="3",Time="2011-01-04"
                          },new HourlyWeather()
                          {
                                Temperature="4",Time="2011-01-05"
                          }
                      }
                }
            };
        }
    }

MVVMLocator.cs

class MVVMLocator
    {
        public MVVMLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
            SimpleIoc.Default.Register<YourVM>();
        }

        public static YourVM YourVM => SimpleIoc.Default.GetInstance<YourVM>();
    }

App.xaml

<Application
    x:Class="App2.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App2">
    <Application.Resources>
        <ResourceDictionary>
            <local:MVVMLocator x:Key="Locator"/>
        </ResourceDictionary>
    </Application.Resources>
</Application>

MainPage.xaml

<Page
    x:Class="App2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <ListView ItemsSource="{Binding YourVM.DailyWeather,Source={StaticResource Locator}}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Height="200">

                        <TextBlock Text="{Binding DayOfYear}"/>

                        <ListView ItemsSource="{Binding HourlyWeather}">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <TextBlock Text="{Binding Temperature}"/>
                                        <TextBlock Text="{Binding Time}"/>
                                    </StackPanel>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>

                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Page>

Nuget:CommonServiceLocator,MvvmLightLibs

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。