问题
I have a list of buttons:
<Button Style="{DynamicResource DestinationButtonStyle}">
<TextBlock Text="{Binding FullName}"
Style="{DynamicResource DestinationStationTextBlockStyle}"
TextTrimming="CharacterEllipsis" />
</Button>
I want buttons to have names equal to text of content. Is it possible to make this kind of binding?
回答1:
Quote from the MSDN:
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 short, the Binding
Name property for control is impossible and undesirable. But you can use the attached dependency property, like this:
<Button local:GiveName.Name="{Binding FullName}" ... />
Or you can use the Tag
of Button
, for Tag
Binding works.
回答2:
You shouldn't do that. There are several restrictions for Name
property. As MSDN says:
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.
来源:https://stackoverflow.com/questions/18585238/how-to-bind-button-name-to-content