如何通过数据绑定设置WPF超链接的文本? [英] How can I set the text of a WPF Hyperlink via data binding?

查看:239
本文介绍了如何通过数据绑定设置WPF超链接的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中,我想创建一个导航到一个对象的细节的超链接,我想要超链接的文本是对象的名称。现在,我有这样的:

 < 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆