从dataTemplate资源访问父绑定上下文(x:参考)

时间:2018-12-08 14:59:35

标签: data-binding xamarin.forms datatemplate controltemplate

我得到一个页面,其中考虑了绑定到viewModel中的属性的一些业务逻辑,从而加载了多个控制模板。

<ContentView x:Name="ContentView"  ControlTemplate="{Binding ItemControlTemplate}">
</ContentView>

在此控件模板中,我得到了一个使用DataTemplate的CarouselViewControl。

<carousel:CarouselViewControl x:Name="CarouselViewMapTemplate" Position=" 
    {TemplateBinding BindingContext.CarouselPosition}" 
    PositionSelectedCommand="{TemplateBinding BindingContext.PositionChanged}" 
    Orientation="Horizontal" AnimateTransition="True" IsSwipeEnabled="False" 
    ItemsSource="{TemplateBinding BindingContext.Items}" InterPageSpacing="10" 
    ItemTemplate="{StaticResource ItemsDataTemplate }">
</carousel:CarouselViewControl>

在这个DataTemplate中,我想绑定到ViewModel中的一个属性,它也是一个DataTemplate。

 <maps:SfMaps  x:Name="sfmap" EnablePanning="False" EnableZooming="False" BackgroundColor="Transparent">
            <maps:SfMaps.Layers>
                <maps:ShapeFileLayer x:Name="ShapeLayer" Uri="FRA_adm0.shp" MarkerTemplate="{Binding Parent.BindingContext.MarkerDataTemplate}">

                </maps:ShapeFileLayer>
            </maps:SfMaps.Layers>
        </maps:SfMaps>

我该如何实现?我刚刚尝试过

  

{Binding Parent.BindingContext.MyProperty}

但是这不起作用。我还研究了绑定的Source属性,但是不会这样做,因为我的DataTemplate中没有引用,在应用程序级别ResourceDictionary中用作资源。

此DOESNT的工作是在ResourceDictionary中找不到carouselView:

"{Binding Path=BindingContext.DisplayMarkerTemplateCommand, Source={x:Reference carouselView}}"

如果绑定不能在XAML中正常工作,则无法做到这一点将破坏ResourceDictionary的整体结构并清理模板。

谢谢!

0 个答案:

没有答案