依赖属性到嵌套网格可见性?

时间:2016-01-08 09:37:10

标签: c# wpf xaml dependency-properties

我有一个公开地图控件的视图:

 <controls:OnlineMapControl                                     
       x:Name="OnlineMapControl"                                        
       PanVisibility="Collapsed"
       Margin="0,-5,0,0"
       Background="{DynamicResource DocumentAreaBrush}">
 </controls:OnlineMapControl>

在我的OnlineMapControl用户控件中,代码包含另一个userControl。我试图设置这个二次控件的可见性:

<onlineMapControls:NavigationControls Map="{Binding ElementName=tileCanvas}" Visibility="{Binding PanVisibility, RelativeSource={RelativeSource TemplatedParent}}" />

在OnlineMapControl背后的代码中,我公开了一个依赖属性:

        /// <summary>
        /// Identifies the PanVisibility dependency property.
        /// </summary>
        public static readonly DependencyProperty PanVisibilityProperty =
            DependencyProperty.Register("PanVisibility", typeof(Visibility), typeof(OnlineMapControl));

        /// <summary>
        /// Gets or sets the PanVisibility.
        /// </summary>
        public Visibility PanVisibility
        {
            get { return (Visibility)GetValue(PanVisibilityProperty); }
            set { SetValue(PanVisibilityProperty, value); }
        }

在我看来,我正在获得“会员”的PanVisibility&#39;不被承认或不可访问&#39;我在这做错了什么?

1 个答案:

答案 0 :(得分:1)

TemplatedParent仅可用于ControlTemplate,也可用于您所说的未定义任何ControlTemplate,仅用于UserControl的内容。您应该使用FindAncestor模式:

Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:OnlineMapControl}}, Path=PanVisibility}"