WPF 中控件模板的动态名称?
我们需要在运行时使用数据绑定分配 Name 属性。我们已经定义了控件模板,并且 WPF
不允许从数据绑定对象绑定名称。
如果我这样做,我会得到类似的错误,
错误 3 标记扩展不是 允许 Uid 或 Name 属性 值,因此“{Binding Name}”不是 有效的。 15号线位置 36. C:\Users\gsubramani\Workspace\ProLinkIII_Experiment\BasicFrame\Resources\TreeTemplate.xaml 15 36 BasicFrame
要解决此问题,是否可以定义一个自定义属性,以便通过数据绑定来设置控件的 Name 属性?
我不知道如何处理 WPF
中的 DependencyProperty
以便我可以定义一个自定义属性来实习设置控件的 Name 属性? (我对 WPF 世界非常陌生)。
We need to assign the Name property at the runtime using the data binding. We have the control template defined and WPF
doesn't allow to bind the name from the the Data Bind object.
If I do, I get error like,
Error 3 MarkupExtensions are not
allowed for Uid or Name property
values, so '{Binding Name}' is not
valid. Line 15 Position
36. C:\Users\gsubramani\Workspace\ProLinkIII_Experiment\BasicFrame\Resources\TreeTemplate.xaml 15 36 BasicFrame
To work around, Is it possible to define a custom property which intern set the Name property of the control through data binding?
I don't know how to the handle the DependencyProperty
in WPF
so that I can define a custom property that intern set the Name property of the control? ( I am very new to WPF world).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您需要此 UI 自动化,我建议使用此处描述的解决方案:
http://www.jonathanantoine.com/2011/11/03/coded-ui-tests-automationid-or-how-to-find-the-chose-one-control/
您可以使用AutomationProperties.AutomationId 在 WPF 中具有绑定,您可以在 UI 自动化客户端中使用该值作为自动化 ID。
If you need this for UI Automation, I'd recommend using the solution described here:
http://www.jonathanantoine.com/2011/11/03/coded-ui-tests-automationid-or-how-to-find-the-chose-one-control/
You can use the AutomationProperties.AutomationId with binding in WPF, and you can use this value as automationid in your UI Automation client.
您需要按照此处的文档定义附加依赖项:
http://msdn.microsoft.com/en-us/library/cc903943%28VS.95%29.aspx
然后提供一个方法,当此属性更改时将调用该方法。您可以通过向 propertyMetdata 添加另一个参数来实现此目的,该参数指向静态方法:
在此处理程序中,d 是属性已附加到的对象,e.NewValue 将包含名称。
但是,我认为您想要绑定元素的名称有点奇怪。您能分享一下为什么要这样做吗?
问候,科林·E.
You would need to define an attached dependency as per the documentation here:
http://msdn.microsoft.com/en-us/library/cc903943%28VS.95%29.aspx
Then provide a method which is invoked when this property changes. You can do this by adding another argument to the propertyMetdata, that points to a static method:
In this handler, d, is the object that the property has been attached to, and e.NewValue will contain the name.
However, I think it is a little odd that you want to bind the Name of your elements. Can you please share why you want to do this?
Regards, Colin E.