我试图让 CarouselView 中的每个视图都具有与父 ContentPage 相同的 BindingContext 对象。我尝试了以下似乎不起作用的方法。MainPage.xaml 是在运行时初始化的页面。它拥有一个 CarouselView,其中有四个 ContentViews 拥有我可以在 MainPage 之间来回滑动的功能“页面”。
MainPage.xaml:
...
<CarouselView Grid.Row="1" Grid.ColumnSpan="4"
Position="{Binding CarouselPosition}"
HorizontalScrollBarVisibility="Never">
<CarouselView.ItemsSource>
<x:Array Type="{x:Type ContentView}">
<local:HomeView></local:HomeView>
<local:NewsView></local:NewsView>
<local:ChartsView></local:ChartsView>
<local:SettingsView></local:SettingsView>
</x:Array>
</CarouselView.ItemsSource>
<CarouselView.ItemTemplate>
<DataTemplate>
<ContentView Content="{Binding .}" BindingContext="{Binding BindingContext, Source={x:Reference mainPage}}"/>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
...
基本上,我希望能够将这些 ContentView 中的每一个的 BindingContext 设置为与父级相同。我还读到 ContentViews 有时应该能够继承 BindingContext,但在这种情况下似乎没有发生。
谢谢。