如何通过数据绑定设置WPF超链接的文本? [英] How can I set the text of a WPF Hyperlink via data binding?
问题描述
< TextBlock>< Hyperlink Command =local:MyCommands.ViewDetailsCommandParameter = {Binding}>对象名称< / Hyperlink>< / TextBlock>
但是我想要将对象名称绑定到对象的实际名称。我想做这样的事情:
< TextBlock>< Hyperlink Command =local:MyCommands.ViewDetailsCommandParameter ={Binding}Text ={Binding Path = Name}/>< / TextBlock>
但是,超链接类没有适合数据绑定的文本或内容属性(该是,依赖属性)。
任何想法?
看起来很奇怪,但它有效。我们在大约20个不同的地方在我们的应用程序中做到这一点。 超链接
隐式地构造一个< Run />
如果你把文本放在其内容,而在.NET 3.5 <运行/>
不会让你绑定它,所以你必须明确地使用一个 TextBlock
。
< TextBlock>
< Hyperlink Command =local:MyCommands.ViewDetailsCommandParameter ={Binding}>
< TextBlock Text ={Binding Path = Name}/>
< / Hyperlink>
< / TextBlock>
更新:注意从.NET 4.0开始, Run.Text属性现在可以绑定:
< Run Text ={Binding Path = Name} />
In WPF, I want to create a hyperlink that navigates to the details of an object, and I want the text of the hyperlink to be the name of the object. Right now, I have this:
<TextBlock><Hyperlink Command="local:MyCommands.ViewDetails" CommandParameter="{Binding}">Object Name</Hyperlink></TextBlock>
But I want "Object Name" to be bound to the actual name of the object. I would like to do something like this:
<TextBlock><Hyperlink Command="local:MyCommands.ViewDetails" CommandParameter="{Binding}" Text="{Binding Path=Name}"/></TextBlock>
However, the Hyperlink class does not have a text or content property that is suitable for data binding (that is, a dependency property).
Any ideas?
It looks strange, but it works. We do it in about 20 different places in our app. Hyperlink
implicitly constructs a <Run/>
if you put text in its "content", but in .NET 3.5 <Run/>
won't let you bind to it, so you've got to explicitly use a TextBlock
.
<TextBlock>
<Hyperlink Command="local:MyCommands.ViewDetails" CommandParameter="{Binding}">
<TextBlock Text="{Binding Path=Name}"/>
</Hyperlink>
</TextBlock>
Update: Note that as of .NET 4.0 the Run.Text property can now be bound:
<Run Text="{Binding Path=Name}" />
这篇关于如何通过数据绑定设置WPF超链接的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!