我们需要在运行时使用数据绑定(bind)分配 Name 属性。我们定义了控件模板,WPF
不允许绑定(bind)来自 Data Bind 对象的名称。
如果我这样做,我会得到这样的错误,
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
要解决这个问题,是否可以定义一个自定义属性,让实习生通过数据绑定(bind)设置控件的 Name 属性?
我不知道如何处理 WPF
中的 DependencyProperty
,以便我可以定义一个自定义属性,实习生设置控件的 Name 属性? (我对 WPF 世界还很陌生)。
最佳答案
如果您需要它来实现 UI 自动化,我建议您使用此处描述的解决方案: http://www.jonathanantoine.com/2011/11/03/coded-ui-tests-automationid-or-how-to-find-the-chose-one-control/
您可以在 WPF 中将 AutomationProperties.AutomationId 与绑定(bind)一起使用,并且可以在您的 UI 自动化客户端中将此值用作 automationid。
关于c# - WPF 中控件模板的动态名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4469870/