【发布时间】:2013-03-22 04:30:54
【问题描述】:
问题:使用 XAML,当值是复杂值时,如何直接在 Window 上设置 Attached Property?
说明:
请注意,为简洁起见,我已在各处删除了一些 XAML 代码。
我正在尝试直接在 WPF 窗口上设置附加属性。通常,它可以像这样设置为窗口的属性:
<Window
xmlns:data="clr-namespace:MVVM_Test.Data"
data:AttachedProperties.RegisterCommandBindings="somevalue" />
如果属性只需要一个简单的值(甚至是简单的绑定),这很好。但是,我想使用 MultiBinding 将附加属性设置为复杂值。当我使附加属性成为我的网格的成员时,我有这个工作:
<Window>
<Window.Resources>
<data:BindingConverter x:Key="RegisterCommandBindingsConverter" />
</Window.Resources>
<Grid>
<data:AttachedProperties.RegisterCommandBindings>
<MultiBinding Converter="{StaticResource RegisterCommandBindingsConverter}">
<Binding RelativeSource="{RelativeSource Mode=Self}" Path="(data:AttachedProperties.BaseBindings)" />
<Binding ElementName="automobileView" Path="DataContext.CommandBindings" />
</MultiBinding>
</data:AttachedProperties.RegisterCommandBindings>
但是,我希望附加属性驻留在 Window 上,而不是 Grid 上。虽然 Grid 上的 Attached 属性完全符合我的需要,但让我感到困扰的是,我无法弄清楚如何在 Window 上设置它。
如果我将 Attached Property 绑定作为 Window 的第一个成员,在 Window.Resources 之前,我会从 Window 的 XAML 中得到一个运行时异常,说明:
'在 'System.Windows.StaticResourceExtension' 上提供值引发了异常。'行号“6”和行位置“9”。
内部例外:
找不到名为“RegisterCommandBindingsConverter”的资源。资源名称区分大小写。
如果我将附加属性绑定放在 Window.Resources 之后但仍作为 Window 的直接成员和 Grid 之前,我会在编译时收到以下错误:
对象 'Window' 已经有一个子对象,不能添加 ''。 'Window' 只能接受一个孩子。第 42 行第 11 行。
【问题讨论】:
标签: wpf xaml data-binding attached-properties