【问题标题】:How can I set the text of a WPF Hyperlink via data binding?如何通过数据绑定设置 WPF 超链接的文本?
【发布时间】:2010-09-13 13:11:27
【问题描述】:

在 WPF 中,我想创建一个导航到对象详细信息的超链接,并且我希望超链接的文本是对象的名称。现在,我有这个:

<TextBlock><Hyperlink Command="local:MyCommands.ViewDetails" CommandParameter="{Binding}">Object Name</Hyperlink></TextBlock>

但我希望将“对象名称”绑定到对象的实际名称。我想做这样的事情:

<TextBlock><Hyperlink Command="local:MyCommands.ViewDetails" CommandParameter="{Binding}" Text="{Binding Path=Name}"/></TextBlock>

但是,Hyperlink 类没有适合数据绑定的文本或内容属性(即依赖属性)。

有什么想法吗?

【问题讨论】:

    标签: wpf data-binding hyperlink


    【解决方案1】:

    它看起来很奇怪,但它确实有效。我们在我们的应用程序中的大约 20 个不同的地方执行此操作。 Hyperlink 隐式构造 &lt;Run/&gt; 如果您将文本放入其“内容”中,但在 .NET 3.5 中 &lt;Run/&gt; 不允许您绑定到它,因此您必须显式使用 TextBlock

    <TextBlock>
        <Hyperlink Command="local:MyCommands.ViewDetails" CommandParameter="{Binding}">
            <TextBlock Text="{Binding Path=Name}"/>
        </Hyperlink>
    </TextBlock>
    

    更新:请注意,从 .NET 4.0 开始,现在可以绑定 Run.Text property

    <Run Text="{Binding Path=Name}" />
    

    【讨论】:

    • 那么,这是否意味着超链接的内容属性是内联集合?
    • 只是我还是这会阻止链接实际工作?
    • 你还需要处理RequestNavigate
    【解决方案2】:

    这在“页面”中对我有用。

    <TextBlock>
        <Hyperlink NavigateUri="{Binding Path}">
            <TextBlock Text="{Binding Path=Path}" />
        </Hyperlink>
    </TextBlock>
    

    【讨论】:

      【解决方案3】:

      在 Windows Store 应用程序(和 Windows Phone 8.1 RT 应用程序)上,上面的示例不起作用,请使用 HyperlinkBut​​ton 并像往常一样绑定 Content 和 NavigateUri 属性。

      猜你喜欢
      • 2012-09-07
      • 2018-06-02
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 2011-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多