Xaml Xamarin表单:如何引用父绑定

Xaml Xamarin表单:如何引用父绑定,xaml,xamarin,binding,binding-context,Xaml,Xamarin,Binding,Binding Context,假设此viewcell位于ListView内。如果内容页与视图模型绑定,如何获取对内容页绑定的引用。当前,“ABC”正在引用列表中某个对象的属性,但我想从内容页的bindingcontext中获取该值 <ViewCell> <ViewCell.View> <Label Text="{Binding ABC}"></Label> </ViewCell.View> </ViewCell> 您需要在标签

假设此viewcell位于ListView内。如果内容页与视图模型绑定,如何获取对内容页绑定的引用。当前,“ABC”正在引用列表中某个对象的属性,但我想从内容页的bindingcontext中获取该值

<ViewCell> 
   <ViewCell.View>
      <Label Text="{Binding ABC}"></Label>
   </ViewCell.View>
</ViewCell>

您需要在标签内部添加
BindingContext=“{x:Reference viewmodel}

<ffimageloading:CachedImage.GestureRecognizers>
   <TapGestureRecognizer BindingContext="{x:Reference page}" Command="{Binding OnSignInCommand}" CommandParameter="{Binding Model}" />
</ffimageloading:CachedImage.GestureRecognizers>

这就是描述它的原因。

即使是库布斯也给出了正确的答案。我想用例子来回答这个问题,让它更清楚

请考虑我们有一个页面

<ContentPage.BindingContext>
    <mvvm:MasterPageModel 
    x:Name="viewmodel"/>
</ContentPage.BindingContext>
此引用父上下文
x:Class=“你的班级名称”>
//这来自项目来源
/数据模板>
您的视图模型应该是这样的

<ContentPage  
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Name="firstPage" -->this reference parent context
    x:Class="Your_Class_Name">
  <ListView x:Name="ListSource"
            ItemsSource="{Binding ListSource}" >
            <ListView.ItemTemplate>
               <DataTemplate>
                   <ViewCell>
                        <Grid>
                         // this come from item source
                    <Label Text="{Binding ABC}"></Label>
                    <Button Command="{Binding BindingContext.CommandFromParent
                           , Source={x:Reference firstPage} }" />
                        </Grid>

                       </ViewCell>
                  /DataTemplate>
           </ListView.ItemTemplate>
    </ListView>


</ContentPage>
public类ViewModelName
{
私有列表_listSource=新列表();
公共列表列表源
{
get=>\u listSource;
设置
{
_listSource=值;
RaisePropertyChanged();
}
}
public ICommand CommandFromParent=>new命令(HandleYourActionHere);
}
}

当我们编写
BindingContext.CommandFromParent
BindingContext代表firstPage(x:Name=“firstPage”)的BindingContext,它是ViewModelName

DataContext.Command,对我来说很有用


您可以使用
相对资源绑定到祖先

换行


对此


不要忘记在文件顶部添加
viewModel
的xml名称空间:

xmlns:viewModel=“clr命名空间:YourProject.ViewModels”
您可以在此处阅读有关
相对资源
绑定的所有信息:
为内容页命名:

 public class ViewModelName 
    {
        private List<YourDataType> _listSource = new List<YourDataType>();


        public List<YourDataType> ListSource
        {
            get => _listSource;
            set
            {
                _listSource = value;
                RaisePropertyChanged();
            }
        }

        public ICommand CommandFromParent => new Command(HandleYourActionHere);

}
}

访问页面的绑定上下文,如下所示:

<ContentPage x:Name="this">


My OnSignInCommand未被调用。OnSignInCommand是在页面绑定上下文的viewmodel中实现的。CommandParameters中的as模型是该项的绑定对象。请放置所有xaml,并且可能需要添加对不同父项的引用。显示CommandFromParent的Upvoted可能重复作为ViewModel上的一个属性,而不是一些花哨的XAML syntaxIn Xamarin,您需要使用
BindingContext
而不是
DataContext
<ContentPage x:Name="this">
  <Label BindingContext="{Binding Source={x:Reference this}, Path=BindingContext}" >