嵌套网格可见性的依赖属性

本文关键字:依赖 属性 可见性 网格 嵌套 | 更新日期: 2023-09-27 18:29:32

我有一个显示映射控件的视图:

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

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

<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"不被识别或无法访问"我在这里做错了什么?

嵌套网格可见性的依赖属性

TemplatedPrent仅在ControlTemplate中可用,并且根据您所说,您没有定义任何ControlTemplate,只有Content到UserControl。您应该使用FindAncestor模式:

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