为什么这个属性不可见?说找不到可附加的属性
我正在创建带有附加属性的行为。行为应该附加到网格:
public class InteractionsBehavior : Behavior<Grid>
{
public static readonly DependencyProperty ContainerProperty =
DependencyProperty.RegisterAttached("Container", typeof(Grid), typeof(Grid), new PropertyMetadata(null));
public static readonly DependencyProperty InteractionsProviderProperty =
DependencyProperty.RegisterAttached("InteractionsProvider", typeof(IInteractionsProvider), typeof(Grid), new PropertyMetadata(null, OnInteractionsProviderPropertyChanged));
public Grid Container
{
get { return GetValue(ContainerProperty) as Grid; }
set { this.SetValue(ContainerProperty, value); }
}
public IInteractionsProvider InteractionsProvider
{
get { return GetValue(InteractionsProviderProperty) as IInteractionsProvider; }
set { this.SetValue(InteractionsProviderProperty, value); }
}
现在,当我像这样编写 XAML 时,我会收到错误:
<Grid Background="White" x:Name="LayoutRoot"
Behaviors:InteractionsBehavior.InteractionsProvider="{Binding InteractionsProvider}">
错误 4 类型上不存在属性“InteractionsProvider” XML 命名空间中的“网格” 'clr-命名空间:基础设施.行为;程序集=基础设施.SL'。 C:\MainPage.xaml 11 11 Controls.SL.Test
错误 1 未找到可附加属性“InteractionsProvider” 类型 “交互行为”。 C:\MainPage.xaml 11 11 Controls.SL.Test
I'm creating Behavior with attached properties. Behavior should attach to Grid:
public class InteractionsBehavior : Behavior<Grid>
{
public static readonly DependencyProperty ContainerProperty =
DependencyProperty.RegisterAttached("Container", typeof(Grid), typeof(Grid), new PropertyMetadata(null));
public static readonly DependencyProperty InteractionsProviderProperty =
DependencyProperty.RegisterAttached("InteractionsProvider", typeof(IInteractionsProvider), typeof(Grid), new PropertyMetadata(null, OnInteractionsProviderPropertyChanged));
public Grid Container
{
get { return GetValue(ContainerProperty) as Grid; }
set { this.SetValue(ContainerProperty, value); }
}
public IInteractionsProvider InteractionsProvider
{
get { return GetValue(InteractionsProviderProperty) as IInteractionsProvider; }
set { this.SetValue(InteractionsProviderProperty, value); }
}
Now when I'm writing XAML like this I get error:
<Grid Background="White" x:Name="LayoutRoot"
Behaviors:InteractionsBehavior.InteractionsProvider="{Binding InteractionsProvider}">
Error 4 The property 'InteractionsProvider' does not exist on the type
'Grid' in the XML namespace
'clr-namespace:Infrastructure.Behaviors;assembly=Infrastructure.SL'. C:\MainPage.xaml 11 11 Controls.SL.TestError 1 The attachable property 'InteractionsProvider' was not found
in type
'InteractionsBehavior'. C:\MainPage.xaml 11 11 Controls.SL.Test
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您指定它只能由
InteractionsBehavior
附加(“拥有”)。如果您希望能够将其分配给网格,请将 RegisterAttached 行更改为:(或使用 Grid 的类层次结构中的某个基类...)
You specified that it only should be available to be attached ("owned") by an
InteractionsBehavior
. If you want to be able to assign this to a grid, change the RegisterAttached line to:(Or use some base class in
Grid
's class hierarchy...)问题出在你的附加财产的声明上。附加属性有 4 部分:名称、类型、所有者类型和属性元数据。您指定 InteractionsProvider 属性由 Grid 类型拥有(并因此提供)。实际情况并非如此。将所有者类型(第三个参数)更改为
typeof(InteractionsBehavior)
(您在其中声明附加属性的类),切换到静态 get/set 方法而不是属性(因为您是使用附加属性,而不是依赖属性),并且它应该全部按您的预期工作。The problem is in the declaration of your attached property. Attached properties have 4 parts: the name, the type, the owner type, and the property metadata. You're specifying that the InteractionsProvider property is owned (and thus supplied) by the type Grid. That's not actually the case. Change the owner type (third parameter) to
typeof(InteractionsBehavior)
(the class in which you've declared the attached property), switch to static get/set methods instead of a property (because you're using an attached property, not a dependency property), and it should all work as you expect.