charlie smith
charlie smith

Reputation: 11

Wpf bind string to the name property

I have a button in a dynamically loaded XAML file.

<Button Name="{Binding Template_Text1}"
        Width="800" Height="76"
        Content="{Binding Template_Text1}"
        Style="{DynamicResource RoundedButton}"/>

If I give a static NAME to the button everything goes ok. so, how can I bind a string variable to the NAME property?

Upvotes: 1

Views: 2268

Answers (2)

Vinit Sankhe
Vinit Sankhe

Reputation: 19885

Keeping @Erno's valuable input in mind above, I think you can change Name of a property via binding through an attached behavior.

   <Button local:NameAnimationBehavior.Name="{Binding Template_Text1}" ... />

And inside the NameAnimationBehavior, in NameProperty's dependency property changed handler, change the sender's Name property with e.NewValue.

Upvotes: 2

Emond
Emond

Reputation: 50672

From the MSDN:

Name is one of the very few dependency properties that cannot be animated ( IsAnimationProhibited is true in metadata), because the name itself is vital for targeting an animation. Data binding a Name is technically possible, but is an extremely uncommon scenario because a data-bound Name cannot serve the main intended purpose of the property: to provide an identifier connection point for code-behind.

In other words: what you are doing is very tricky so think again whether or not you really need it.

Upvotes: 2

Related Questions