0
WPFハイパーリンクを(データバインドされた)テキストコンテンツにProcess.Start
を実行させる簡単な方法は何ですか?バインドされたwpfハイパーリンクからプロセスを開始
WPFハイパーリンクを(データバインドされた)テキストコンテンツにProcess.Start
を実行させる簡単な方法は何ですか?バインドされたwpfハイパーリンクからプロセスを開始
これは、UserControlや静的なクラスやValueConverterなどにリファクタリングして改善することもできます。ハイパーリンクをViewModel上のコマンドにバインドし、ハイパーリンクのコンテンツを渡すことです(私はできませんでした)相対バインディングを行う方法を見つける)をコマンド・パラメーターとして使用します。
WPFは
<TextBlock>
<TextBlock.DataContext>
<ViewModels:ViewModel />
</TextBlock.DataContext>
<Hyperlink Command="{Binding LaunchHyperlinkCommand}" CommandParameter="{Binding ElementName=_content, Path=Text}">
<Run Text="{Binding FilePath, Mode=OneWay}" Name="_content"/>
</Hyperlink>
</TextBlock>
C#1のDataContextクラススニペット(注:Prismを使用して)スニペット
public class ViewModel : NotificationObject
{
public string FilePath { get; private set; }
public DelegateCommand<string> LaunchHyperlinkCommand { get; set; }
public ViewModel()
{
LaunchHyperlinkCommand = new DelegateCommand<string>(LaunchHyperlink);
}
private static void LaunchHyperlink(string link)
{
try
{
Process.Start(link);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}