开发者

Dynamic Name to the Control Template in 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 t开发者_如何学编程he 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).


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.


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:

private static void OnMyNamePropertyChanged(DependencyObject d,
    DependencyPropertyChangedEventArgs e)
{
    // d is the object which the attached property has been set on
}

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜