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:
在WPF中,我想创建一个超链接,以导航到一个对象的详细信息,并且我希望超链接的文本成为对象的名称。现在,我有:
<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?
什么好主意吗?
3 个解决方案
#1
187
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
.
它看起来很奇怪,但它确实有效。在我们的app中,我们在大约20个不同的地方做了这个操作。如果你将文本放到它的“内容”中,超链接会隐式构造一个
<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:
更新:注意,从。net 4.0开始运行。文本属性现在可以绑定:
<Run Text="{Binding Path=Name}" />
#2
7
This worked for me in a "Page".
这在我的“页面”中起了作用。
<TextBlock>
<Hyperlink NavigateUri="{Binding Path}">
<TextBlock Text="{Binding Path=Path}"/>
</Hyperlink></TextBlock>
#3
1
On Windows Store app (and Windows Phone 8.1 RT app) above example does not work, use HyperlinkButton and bind Content and NavigateUri properties as ususal.
在Windows Store app(和Windows Phone 8.1 RT app)上,上面的例子不起作用,使用HyperlinkButton绑定内容和NavigateUri属性作为usal。
#1
187
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
.
它看起来很奇怪,但它确实有效。在我们的app中,我们在大约20个不同的地方做了这个操作。如果你将文本放到它的“内容”中,超链接会隐式构造一个
<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:
更新:注意,从。net 4.0开始运行。文本属性现在可以绑定:
<Run Text="{Binding Path=Name}" />
#2
7
This worked for me in a "Page".
这在我的“页面”中起了作用。
<TextBlock>
<Hyperlink NavigateUri="{Binding Path}">
<TextBlock Text="{Binding Path=Path}"/>
</Hyperlink></TextBlock>
#3
1
On Windows Store app (and Windows Phone 8.1 RT app) above example does not work, use HyperlinkButton and bind Content and NavigateUri properties as ususal.
在Windows Store app(和Windows Phone 8.1 RT app)上,上面的例子不起作用,使用HyperlinkButton绑定内容和NavigateUri属性作为usal。