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

假设这个 viewcell 在 ListView 中。如果内容页面与视图模型绑定,我如何获得对内容页面绑定的引用。目前,“ABC”正在引用列表中对象的属性,但我想从内容页面的绑定上下文中获取值。

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

5 回答 5

34

@qubuss 在下面给出了正确答案,但我想提供更多上下文并展示一个示例以使其更清楚:

让我们考虑以下页面:

<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 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);

}
}

解释 当我们写的时候BindingContext.CommandFromParent,BindingContext 代表 BindingContextfirstPage(x:Name="firstPage")的类型ViewModelName

于 2018-10-21T19:43:00.577 回答
20

您可以使用RelativeSource.

更改此行

<Label Text="{Binding ABC}"></Label>

对此

<Label Text="{Binding ABC, Source={RelativeSource AncestorType={x:Type viewModel:YourViewModelName}}}"></Label>

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

xmlns:viewModel="clr-namespace:YourProject.ViewModels"

您可以在此处阅读有关RelativeSource绑定的所有信息: https ://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/relative-bindings#bind-to-an-ancestor

于 2020-05-20T10:57:32.823 回答
6

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

<ViewCell> 
  <ViewCell.View>
    <Label Text="{Binding ABC}" BindingContext="{x:Reference Name_Of_Parent}"></Label>
  </ViewCell.View>
</ViewCell>

在 Name_Of_Parent 你把组件的名称。如果你使用 MVVM 和 ViewModel 类,你必须添加x:Name到你的绑定上下文中:

<ContentPage.BindingContext>
    <mvvm:MasterPageModel 
    x:Name="viewmodel"/>
</ContentPage.BindingContext>

这是描述它的文档。

于 2018-01-18T07:19:50.890 回答
3

为内容页面命名:

<ContentPage x:Name="this">

像这样访问页面的绑定上下文:

  <Label BindingContext="{Binding Source={x:Reference this}, Path=BindingContext}" >
于 2020-06-30T11:43:32.423 回答
2

DataContext.Command 对我有用。

<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Name="firstPage"
    x:Class="Your_Class_Name">
    <ListView x:Name="ListSource" ItemsSource="{Binding ListSource}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid>
                        <Label Text="{Binding ABC}"></Label>
                        <Button Command="{Binding BindingContext.CommandFromParent, Source={x:Reference firstPage} }" />
                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage>
于 2019-11-20T12:14:33.970 回答