xamarin 表单可绑定 属性 无法正常工作
xamarin forms bindable property does not work properly
我正在尝试创建如下所示的自定义视图
在这里,我试图将一个集合从主页绑定到自定义视图
但这里绑定不正确
<ContentView.Content>
<Grid
x:Name="Layout"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackLayout
Grid.Row="0"
BackgroundColor="#E0E0E0"
Padding="20,10">
<Label Text="{Binding Source={x:Reference ExpandableContentView}, Path=Header}"/>
</StackLayout>
<StackLayout
Grid.Row="1"
BindableLayout.ItemsSource="{Binding Source={x:Reference ExpandableContentView}, Path=SummeryList}">
<Label Text="{Binding Test}"/>
</StackLayout>
</Grid>
</ContentView.Content>
xmal.cs 文件
public partial class ExpandableView : ContentView
{
public static readonly BindableProperty HeaderProperty = BindableProperty.Create(
nameof(Header),
typeof(string),
typeof(ExpandableView),
default(string));
public static readonly BindableProperty SummeryListProperty = BindableProperty.Create(
nameof(SummeryList),
typeof(ObservableCollection<SummeryDetailModel>),
typeof(ExpandableView),
propertyChanged: CollectionChanged);
private static void CollectionChanged(BindableObject bindable, object oldValue, object newValue)
{
}
public ExpandableView()
{
InitializeComponent();
}
public string Header
{
get => (string)GetValue(HeaderProperty);
set => SetValue(HeaderProperty, value);
}
public ObservableCollection<SummeryDetailModel> SummeryList
{
get => (ObservableCollection<SummeryDetailModel>)GetValue(SummeryListProperty);
set => SetValue(SummeryListProperty, value);
}
我正在 MainPage 中进行绑定,如下所示
<commonviews:ExpandableView
Header="Shipment Details"
SummeryList="{Binding SummeryCollection}">
</commonviews:ExpandableView>
我的 SummeryDetailModel 喜欢
public class SummeryDetailModel
{
public string Test { get; set; }
}
此处未将 Test 值绑定到标签。
我错过了什么吗?
我刚刚弄明白了,我犯了一个小错误。
我没有为 StackLayout BindableLayout[ 添加 Itemtemplate DataTemplate =21=]
如下
<StackLayout
Grid.Row="1"
Padding="20,10"
Spacing="15"
BindableLayout.ItemsSource="{Binding Source={x:Reference ExpandableContentView}, Path=SummeryList}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<StackLayout
Spacing="10">
<Label
Text="{Binding Title}"
FontFamily="{StaticResource LatoRegular}"
FontSize="{StaticResource FontSize14}"
TextColor="{StaticResource PlaceholderColor}"/>
<Label Text="{Binding Detail}"
FontFamily="{StaticResource LatoRegular}"
FontSize="{StaticResource FontSize14}"
TextColor="{StaticResource PrimaryText}"/>
</StackLayout>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
我正在尝试创建如下所示的自定义视图 在这里,我试图将一个集合从主页绑定到自定义视图
但这里绑定不正确
<ContentView.Content>
<Grid
x:Name="Layout"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackLayout
Grid.Row="0"
BackgroundColor="#E0E0E0"
Padding="20,10">
<Label Text="{Binding Source={x:Reference ExpandableContentView}, Path=Header}"/>
</StackLayout>
<StackLayout
Grid.Row="1"
BindableLayout.ItemsSource="{Binding Source={x:Reference ExpandableContentView}, Path=SummeryList}">
<Label Text="{Binding Test}"/>
</StackLayout>
</Grid>
</ContentView.Content>
xmal.cs 文件
public partial class ExpandableView : ContentView
{
public static readonly BindableProperty HeaderProperty = BindableProperty.Create(
nameof(Header),
typeof(string),
typeof(ExpandableView),
default(string));
public static readonly BindableProperty SummeryListProperty = BindableProperty.Create(
nameof(SummeryList),
typeof(ObservableCollection<SummeryDetailModel>),
typeof(ExpandableView),
propertyChanged: CollectionChanged);
private static void CollectionChanged(BindableObject bindable, object oldValue, object newValue)
{
}
public ExpandableView()
{
InitializeComponent();
}
public string Header
{
get => (string)GetValue(HeaderProperty);
set => SetValue(HeaderProperty, value);
}
public ObservableCollection<SummeryDetailModel> SummeryList
{
get => (ObservableCollection<SummeryDetailModel>)GetValue(SummeryListProperty);
set => SetValue(SummeryListProperty, value);
}
我正在 MainPage 中进行绑定,如下所示
<commonviews:ExpandableView
Header="Shipment Details"
SummeryList="{Binding SummeryCollection}">
</commonviews:ExpandableView>
我的 SummeryDetailModel 喜欢
public class SummeryDetailModel
{
public string Test { get; set; }
}
此处未将 Test 值绑定到标签。
我错过了什么吗?
我刚刚弄明白了,我犯了一个小错误。
我没有为 StackLayout BindableLayout[ 添加 Itemtemplate DataTemplate =21=]
如下
<StackLayout
Grid.Row="1"
Padding="20,10"
Spacing="15"
BindableLayout.ItemsSource="{Binding Source={x:Reference ExpandableContentView}, Path=SummeryList}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<StackLayout
Spacing="10">
<Label
Text="{Binding Title}"
FontFamily="{StaticResource LatoRegular}"
FontSize="{StaticResource FontSize14}"
TextColor="{StaticResource PlaceholderColor}"/>
<Label Text="{Binding Detail}"
FontFamily="{StaticResource LatoRegular}"
FontSize="{StaticResource FontSize14}"
TextColor="{StaticResource PrimaryText}"/>
</StackLayout>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>