XAML 中没有“属性”一词的附加属性;
MS 定义了“Grid.RowProperty”和“Grid.ColumnProperty”等附加属性,但在 XAML 中,您只需使用“Grid.Row”和“Grid.Column”来调用它,
我尝试对名为“MyValProperty”的附加属性执行相同的操作在“Foo”类上以名称“MyVal”注册,但 XAML 不允许我输入“Foo.MyVal”,而是让我输入“Foo.MyValProperty” MS 是如何做到这一点的?我缺少什么?
MS defines attached properties like 'Grid.RowProperty' and 'Grid.ColumnProperty', but in XAML you just call it with 'Grid.Row' and 'Grid.Column'
I tried doing the same with an attached property called 'MyValProperty' that was registered with the name 'MyVal' on the class 'Foo', but the XAML won't let me type 'Foo.MyVal' and instead makes me type 'Foo.MyValProperty' How is MS doing that? What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为背景,以下是如何注册附加属性
您还可以使用标准属性格式而不是 Set/Get 构造。这是 WPF 拥有强大约定的领域之一。
AttachedProperty
(或任何DependencyProperty
)由三个部分组成。第一个是使用 DependencyProperty.RegisterAttached 注册属性,并且返回值应设置为名为 [Property Name]Property 的公共静态变量。第二个是注册财产时的第一个参数应该是“[财产名称]”。第三个是用于与外界交互的 Get/Set 方法,应命名为 Get[Property Name]、Set[Property Name]。如果您按照约定进行设置,WPF 将识别这两者是连接的,并且应该允许您按预期使用该属性。
As background here's how register an attached property
You can also use a standard property format rather than the Set/Get construct. This is one of those areas where WPF has a strong convention in place. There are three parts of an
AttachedProperty
(or anyDependencyProperty
). The first is registering the property withDependencyProperty.RegisterAttached
and the return value should be set to a public static variable named [Property Name]Property. The second is that the first argument when registering your property should be "[Property Name]". And the third is the Get/Set methods you'll use to interact with the outside world which should be named Get[Property Name], Set[Property Name].If you set it up according to the convention WPF will recognize that the two are connected and should allow you to use the property as you expect.