【问题标题】:Dependency Property to nested grid visibility?嵌套网格可见性的依赖属性?
【发布时间】:2016-01-08 09:37:09
【问题描述】:

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

 <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'”我在这里做错了什么?

【问题讨论】:

  • 使用TemplateBinding 而不是Binding。或者,尝试在您的 DP 定义中设置 FrameworkPropertyMetadataOptions.Inherits

标签: c# wpf xaml dependency-properties


【解决方案1】:

TemplatedParent 只能在 ControlTemplate 中使用,并且根据您所说的,您没有定义任何 ControlTemplate,只有 UserControl 的内容。你应该使用 FindAncestor 模式:

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

【讨论】:

  • 在RelativeSource中找不到属性Path?
  • @Hardgraf 忘记了括号,添加了它
猜你喜欢
  • 2011-08-19
  • 1970-01-01
  • 1970-01-01
  • 2017-12-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-14
  • 1970-01-01
  • 2011-02-25
相关资源
最近更新 更多