【问题标题】:Binding a ListView Label to a property将 ListView 标签绑定到属性
【发布时间】:2018-10-24 02:40:15
【问题描述】:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:viewModels="clr-namespace:TestApp.ViewModels"
         x:Class="TestApp.Views.MasterPage"
         Title="This is the title">
    <StackLayout>
        <ListView x:Name="listView">
            <ListView.ItemsSource>
                <x:Array Type="{x:Type viewModels:MasterPageItem}">
                    <viewModels:MasterPageItem Title="{Binding Item1}"/>
                    <viewModels:MasterPageItem Title="{Binding Item2}"/>
                </x:Array>
            </ListView.ItemsSource>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Label Text="{Binding Title}" VerticalTextAlignment="End"/>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>

所以我收到一个构建错误:“没有为 'Title' 找到属性、可绑定属性或事件,或者值和属性之间的类型不匹配。”我正在尝试将 MasterPageItem Title 属性绑定到我视图中的一个值(这样做是为了在运行时通过选择所需的语言来翻译值)。 Item1 和 Item2 是位于 MasterPage 视图中的属性,而 Title 位于 MasterPageItem 中。 ListView 中的 Label 被绑定到 MasterPageItem Title 属性。

我不太确定我在这里到底做错了什么,因此我们将不胜感激。

【问题讨论】:

    标签: xaml xamarin


    【解决方案1】:

    如果这个MasterPageItem 指的是在您的项目中添加MasterDetailPage 时通常创建的用于处理菜单选择的类,那么它确实不是BindableProperty

    这是我们使用的默认模型:

    public class MainPageMenuItem
    {
        public MainPageMenuItem()
        {
            TargetType = typeof(MainPageDetail);
        }
        public int Id { get; set; }
        public string Title { get; set; }
    
        public Type TargetType { get; set; }
    }
    

    如果您想使用绑定,您必须将此类设为BindableObject,然后将其属性更改为可绑定的。

    像这样:

    public class MainPageMenuItem : BindableObject
    {
        public MainPageMenuItem()
        {
            TargetType = typeof(MainPageDetail);
        }
    
        public readonly static BindableProperty IdProperty = BindableProperty.Create(nameof(Id), typeof(int), typeof(MainPageMenuItem));
        public int Id
        {
            get { return (int)GetValue(IdProperty); }
            set { SetValue(IdProperty, value); }
        }
    
        public readonly static BindableProperty TitleProperty = BindableProperty.Create(nameof(Title), typeof(string), typeof(MainPageMenuItem));
        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }
    
        public readonly static BindableProperty TargetTypeProperty = BindableProperty.Create(nameof(TargetType), typeof(Type), typeof(MainPageMenuItem));
        public Type TargetType
        {
            get { return (Type)GetValue(TargetTypeProperty); }
            set { SetValue(TargetTypeProperty, value); }
        }
    }
    

    希望对你有帮助。

    【讨论】:

    • 这解决了错误,但该值仍未显示在列表视图中。
    • @tldragoon 所以请确保Item1Item2 有内容和/或正在通知更改。您必须检查数组中每个 viewModels:MasterPageItemBindingContext 是否按预期设置。
    • 因此通知正在通过(更改时正在调用属性)。我不确定我是如何检查 BindingContext 的……
    • 你在这里做一个绑定:viewModels:MasterPageItem Title="{Binding Item1}" 对吗?这个Item1 是从哪里来的?它在页面的视图模型中吗?
    • 现在我只是在页面的 .cs 文件中拥有它
    猜你喜欢
    • 1970-01-01
    • 2011-01-20
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 2015-07-24
    • 2018-09-09
    • 1970-01-01
    相关资源
    最近更新 更多