我有以下Contentpage.content,我在其中设置了某些绑定上下文.
<StackLayout>
<local:Post />
<local:Comments />
</StackLayout>
Run Code Online (Sandbox Code Playgroud)
在Post.xaml.cs(ContentView)中,我试图以这种方式获取ContentPage的绑定上下文,但它不起作用.
BindingContext = (PostViewModel)Parent.BindingContext;
Run Code Online (Sandbox Code Playgroud)
如果我站在ContentView中,如何获取ContentPage的绑定上下文?
我得到了ViewModel一个AddToFavoriteCommand没有被调用的命令().现在它只关注命令,而CustomPin class不是viewModel.我设置我viewModel的BindingContext代码页的后面.
但是因为它枚举了我的customPins集合,所以它会在那里查看命令.我需要回到根.我可能需要更改源但无法使其工作.
<ContentPage.Content>
<StackLayout>
<AbsoluteLayout>
<Button Text="{Binding Filter}" Command="{Binding GotoFilterPageCommand}" />
</AbsoluteLayout>
<ListView x:Name="ListView" RowHeight="60" ItemsSource="{Binding CustomPins}" ItemSelected="OnItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="Favorite" Command="{Binding AddToFavoriteCommand}" />
<MenuItem Text="..." CommandParameter="{Binding .}" Clicked="OnMoreClicked" />
</ViewCell.ContextActions>
<StackLayout Padding="5" Orientation="Vertical" >
<Label Text="{Binding ParkingLot.Street}" FontSize="Medium" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
Run Code Online (Sandbox Code Playgroud)
代码背后(删除了所有其他逻辑,例如我不需要的点击事件)
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ParkingListPage : ContentPage
{
public ParkingListPage()
{
InitializeComponent();
BindingContext = new ParkingListViewModel(); …Run Code Online (Sandbox Code Playgroud)