x:Reference "" 不是 属性 "IsVisible" 的有效值

x:Reference "" is not a valid value for the property "IsVisible"

我对 WPF 比较陌生,非常喜欢在标记中处理 GUI 逻辑的可能性。 目前,我有一个控件,如果另一个控件可见,我只希望它可见。

<AttachedControl IsVisible="{x:Reference Name=mainControl}"/>
<MasterControl Name="mainControl" IsVisible="True">
...
</MasterControl>

当我使用它时,它在设计器中工作,但产生错误消息:

Error "" is no valid value for the property "IsVisible". ProjectX MyUserControl.xaml

它也编译成功,我可以 运行 应用程序。那么谁能告诉我问题出在哪里或者我哪里错了?doing/understanding?

x:Reference 标记扩展引用元素的 x:Name 但您不能将 IsVisible 属性 绑定到具有 x:Name [=32] 的控件=] 本身。您应该绑定到它的布尔值 属性。

只要 mainControl 有一个 IsVisible 属性,即 x:Referencesource绑定和 IsVisible 是路径:

<AttachedControl IsVisible="{Binding IsVisible, Source={x:Reference Name=mainControl}}"/>

您还可以通过设置绑定的 ElementName 属性 来绑定到另一个元素:

<AttachedControl IsVisible="{Binding IsVisible, ElementName=mainControl}"/>

What is the difference between x:Reference and ElementName?