如何在xaml,xamarin中设置bindingcontext

时间:2017-07-19 01:08:29

标签: xamarin binding

以下cs代码可以正常工作。

public partial class StudentDetailPage : ContentPage {
    public StudentDetailViewModel VM { get; set; }
    public StudentDetailPage(Model.Student stu)
    {
        InitializeComponent();
        this.BindingContext = this.VM = new StudentDetailViewModel(stu);
        this.Title = "Detail:"+ stu.Id;
    }  
}

但是我把它放到xaml中,它无法工作,所以如何使用self属性在xaml中编写它

 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:AppVersionUpdate.View"
         x:Class="AppVersionUpdate.View.StudentDetailPage"
         BindingContext="{Binding VM}"
         >
<ContentPage.Content>
    <Grid BindingContext="{Binding Item}" VerticalOptions="Start">
        // some row and colomn define
        <Label Text="Id:"/>
        <Label Text="{Binding Id}" Grid.Column="1"/>
        <Label Text="Name:" Grid.Row="1"/>
        <Label Text="{Binding Name}"  Grid.Row="1" Grid.Column="1"/>
        <Label Text="Score:" Grid.Row="2"/>
        <Label Text="{Binding Score}"  Grid.Row="2" Grid.Column="1"/>
    </Grid>
</ContentPage.Content>

0 个答案:

没有答案
相关问题