Xamarin表单:如何引用父绑定

时间:2018-01-18 06:45:03

标签: xaml xamarin binding binding-context

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

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

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

5 个答案:

答案 0 :(得分:6)

您可以使用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

答案 1 :(得分:3)

即使是qubus都能给出正确的答案,我想用示例来回答这个问题,以使其更加清晰

让我们认为我们有一个页面

<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表示firstPage(x:Name =“ firstPage”)的BindingContext时,它是ViewModelName

答案 2 :(得分:2)

为内容页面命名:

<ContentPage x:Name="this">

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

  <Label BindingContext="{Binding Source={x:Reference this}, Path=BindingContext}" >

答案 3 :(得分:1)

您需要在标签内添加<ViewCell> <ViewCell.View> <Label Text="{Binding ABC}" BindingContext="{x:Reference Name_Of_Parent}"></Label> </ViewCell.View> </ViewCell>

x:Name

在Name_Of_Parent中放入组件名称。如果使用MVVM和ViewModel类,则必须将<ContentPage.BindingContext> <mvvm:MasterPageModel x:Name="viewmodel"/> </ContentPage.BindingContext> 添加到绑定上下文:

with pd.ExcelWriter('test.xls', style_compression = 2) as writer:
    row = 0
    while 1: #running it until I stop it during testing
        data = get_that_sweet_data()
        df = pd.DataFrame(data) #makes a DataFrame object with two rows
        df.to_excel(writer, sheet_name = sheet1, startrow = row)
        row += 3

这是描述它的documentation

答案 4 :(得分:0)

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 DataContext.CommandFromParent, Source={x:Reference firstPage} }" />
                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage>