Xaml 将ListView标签绑定到属性
因此,我得到了一个生成错误:“没有为“Title”找到属性、可绑定属性或事件,或者值和属性之间的类型不匹配。”我正在尝试将MasterPageItem Title属性绑定到视图中的值(这样做是为了在运行时通过所需语言的选择转换值)。Item1和Item2是位于母版页视图中的属性,而Title位于母版页项中。ListView中的标签正绑定到MasterPageItem标题属性Xaml 将ListView标签绑定到属性,xaml,xamarin,Xaml,Xamarin,因此,我得到了一个生成错误:“没有为“Title”找到属性、可绑定属性或事件,或者值和属性之间的类型不匹配。”我正在尝试将MasterPageItem Title属性绑定到视图中的值(这样做是为了在运行时通过所需语言的选择转换值)。Item1和Item2是位于母版页视图中的属性,而Title位于母版页项中。ListView中的标签正绑定到MasterPageItem标题属性 我真的不确定我到底做错了什么,所以我非常感谢您的帮助。如果此MasterPageItem指的是在您的项目上添加Master
我真的不确定我到底做错了什么,所以我非常感谢您的帮助。如果此
MasterPageItem
指的是在您的项目上添加MasterDetailPage
时通常为处理菜单选择而创建的类,那么它实际上没有BindableProperty
这是我们使用的默认模型:
<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>
如果要使用绑定,则必须将此类设置为一个BindableObject
,然后将其属性更改为bindableObjects
像这样:
public class MainPageMenuItem
{
public MainPageMenuItem()
{
TargetType = typeof(MainPageDetail);
}
public int Id { get; set; }
public string Title { get; set; }
public Type TargetType { get; set; }
}
我希望这会有所帮助。这解决了错误,但该值仍然没有显示在列表视图中。@tldragoon因此请确保
Item1
和Item2
有内容和/或正在通知更改。您必须检查数组中每个viewModels:MasterPageItem
的BindingContext
是否按预期进行了设置。因此通知正在发送(更改时会调用属性)。我不太确定如何检查BindingContext,但是…您正在执行绑定:viewModels:MasterPageItem Title=“{binding Item1}”
对吗?这个Item1
是从哪里来的?它在页面的viewmodel中吗?现在我只在页面的.cs文件中有它
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); }
}
}