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

可绑定布局>绑定:在ViewModel上找不到属性X

如何解决可绑定布局>绑定:在ViewModel上找不到属性X

我有一个简单的XAML页面,其代码如下:

<StackLayout BindableLayout.ItemsSource="{Binding Data}">
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <Label BackgroundColor="{Binding Color}"
                   Text="{Binding Text}"
                   FontAttributes="Italic"
                   FontSize="20"
                   VerticalTextAlignment="Center"
                   HorizontalTextAlignment="Center"
                   HeightRequest="105"
                   Margin="25" />
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>

在我后面的查看页面代码中:

public partial class DataView
    {
        public DataView()
        {
            InitializeComponent();

            BindingContext = new viewmodel();
        }
...
}

数据是:

public class viewmodel
{
    public viewmodel()
    {
        Data = new ObservableCollection()<Model> { 
            new Model { Text = "Pink",Color = Color.DeepPink },new Model { Text = "Crimson",Color = Color.Crimson },new Model { Text = "Aqua",Color = Color.Aqua },new Model { Text = "Blue",Color = Color.DeepSkyBlue },new Model { Text = "BurlyWood",Color = Color.BurlyWood },};
    }
 
    public ObservableCollection<Model> Data { get; set; }
}
 
public class Model
{
    public Color Color { get; set; }
    public string Text { get; set; }
}

但是,在编译时,我得到:

绑定:在“ MyComp.viewmodel”上找不到属性“颜色”。

好像它是在viewmodel中而不是父级(数据)中搜索Color。

我正在使用最新的Xamarin.Forms 4.8.0,.NET Standard 2.0和Visual Studio 2019 16.7.4。

解决方法

我测试了xaml代码,它仍然可以正常工作。检查gif:https://us.v-cdn.net/5019960/uploads/editor/pd/aa0fp5pwnvkl.gif

这是相关代码,您可以参考它。

Page.xaml

<StackLayout x:Name="expanderLayout" BindableLayout.ItemsSource="{Binding Data}">
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <Expander>
                <Expander.Header>
                    <Grid>
                        <Label BackgroundColor="{Binding Color}"/>
                    </Grid>
                </Expander.Header>
                <Expander.ContentTemplate>
                    <DataTemplate>
                        <Label
                            Text="{Binding Text}"
                            FontAttributes="Italic"
                            FontSize="20"
                            VerticalTextAlignment="Center"
                            HorizontalTextAlignment="Center"
                            HeightRequest="105"
                            Margin="25" />
                    </DataTemplate>
                </Expander.ContentTemplate>
            </Expander>
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>

Page.xaml.cs

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        BindingContext = new ViewModel();
    }
}

Model类和ViewModel类

public class ViewModel
{
    public ViewModel()
    {
        Data = new System.Collections.ObjectModel.ObservableCollection<Model>() {
                new Model { Text = "Pink",Color = Color.DeepPink },new Model { Text = "Crimson",Color = Color.Crimson },new Model { Text = "Aqua",Color = Color.Aqua },new Model { Text = "Blue",Color = Color.DeepSkyBlue },new Model { Text = "BurlyWood",Color = Color.BurlyWood },};
    }

    public ObservableCollection<Model> Data { get; set; }
}

public class Model
{
    public Color Color { get; set; }
    public string Text { get; set; }
}
,

我尝试运行您的代码,但没有发现任何错误 除了您的问题中有一些错别字外,我有这个

   public ViewModel()
    {
        Data = new ObservableCollection<Model>
        {
            new Model {Text = "Pink",Color = Color.DeepPink},new Model {Text = "Crimson",Color = Color.Crimson},new Model {Text = "Aqua",Color = Color.Aqua},new Model {Text = "Blue",Color = Color.DeepSkyBlue},new Model {Text = "BurlyWood",Color = Color.BurlyWood},};
    }

,也许尝试清理/重建解决方案并再次运行?

enter image description here

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