2011-07-20 11 views

答えて

0

これは、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); 
     } 
    } 
} 
関連する問題