データ バインディングを介して WPF ハイパーリンクのテキストを設定するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/140996

  •  02-07-2019
  •  | 
  •  

質問

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 クラスには、データ バインディングに適したテキスト プロパティまたはコンテンツ プロパティ (つまり、依存関係プロパティ) がありません。

何か案は?

役に立ちましたか?

解決

奇妙に見えますが、動作します。アプリの約20か所で実行します。 Hyperlinkは、<!> quot; content <!> quot;にテキストを入力すると<Run/>を暗黙的に構築しますが、.NET 3.5ではTextBlockにバインドできないため、明示的に<=>を使用します。

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

更新:.NET 4.0以降、 Run.Textプロパティをバインドできるようになりました:

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

他のヒント

これは<!> quot; Page <!> quot;で私にとってはうまくいきました。

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

Windows ストア アプリ (および Windows Phone 8.1 RT アプリ) では、上記の例は機能しません。通常どおり、HyperlinkBut​​ton を使用し、Content プロパティと NavigateUri プロパティをバインドします。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top