C# 单击WPF超链接时打开WPF表单

C# 单击WPF超链接时打开WPF表单,c#,.net,wpf,xaml,hyperlink,C#,.net,Wpf,Xaml,Hyperlink,我想在单击WPF超链接时打开一个新的WPF表单。我见过很多只打开web url的示例,但我想打开一个新的WPF表单。您可以只处理单击事件: <Hyperlink Click="Hyperlink_Click">Link</Hyperlink> 您也可能会因为XAML而发疯: <Hyperlink> <Hyperlink.Style> <Style TargetType="{x:Type Hyperlink}">

我想在单击WPF超链接时打开一个新的WPF表单。我见过很多只打开web url的示例,但我想打开一个新的WPF表单。

您可以只处理单击事件:

<Hyperlink Click="Hyperlink_Click">Link</Hyperlink>

您也可能会因为XAML而发疯:

<Hyperlink>
    <Hyperlink.Style>
        <Style TargetType="{x:Type Hyperlink}">
            <Style.Triggers>
                <EventTrigger RoutedEvent="Hyperlink.Click">
                    <BeginStoryboard>
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
                                <Storyboard.Target>
                                    <local:Dialogue />
                                </Storyboard.Target>
                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}"/>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Hyperlink.Style>
    <Hyperlink.Inlines>
        <Run Text="Open Dialogue"/>
    </Hyperlink.Inlines>
</Hyperlink>

为此采取的行动:

public class CreateDialogAction : TriggerAction<Hyperlink>
{
    public Type Type { get; set; }

    protected override void Invoke(object parameter)
    {
        if (Type != null && Type.IsSubclassOf(typeof(Window)) && Type.GetConstructor(Type.EmptyTypes) != null)
        {
            Window window = Type.GetConstructor(Type.EmptyTypes).Invoke(null) as Window;
            window.Show();
        }
    }
}
公共类CreateDialogAction:TriggerAction
{
公共类型类型{get;set;}
受保护的覆盖无效调用(对象参数)
{
if(Type!=null&&Type.IsSubclassOf(typeof(Window))&&Type.GetConstructor(Type.EmptyTypes)!=null)
{
Window Window=Type.GetConstructor(Type.EmptyTypes).Invoke(null)作为窗口;
window.Show();
}
}
}

您可以这样实现:

<Label Height="25" Margin="26,27,116,0" Name="label1" VerticalAlignment="Top">
    <Hyperlink Click="Hyperlink_Click">Click Me</Hyperlink>
</Label>
private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
    Window2 form2 = new Window2();
    form2.Show();
}
XAML:

<TextBlock Height="23" HorizontalAlignment="Left" Margin="254,130,0,0" Name="textBlock1" VerticalAlignment="Top">
        <Hyperlink Click="Hyperlink_Click">Clique Aqui</Hyperlink>
        </TextBlock>

这?

并且使用MVVM可以在您的视图中执行

<Hyperlink NavigateUri="{Binding MyUri}" 
           Command="{Binding OpenHyperlinkCommand}">Link text
</Hyperlink>
链接文本
在您的ViewModel中

private ICommand _openHyperlinkCommand;
public ICommand OpenHyperlinkCommand {
    get
    {
        if (_openHyperlinkCommand == null) 
            _openHyperlinkCommand = new RelayCommand<object>(p => ExecuteHyperlink());
        return _openHyperlinkCommand;
    }
}

private void ExecuteHyperlink() {
    //do stuff here
}
private ICommand\u openhyperlink命令;
公共ICommand openhyperlink命令{
得到
{
如果(_openHyperlinkCommand==null)
_openHyperlinkCommand=newrelaycommand(p=>ExecuteHyperlink());
返回OpenHyperlink命令;
}
}
私有void ExecuteHyperlink(){
//在这里做事
}
<TextBlock Height="23" HorizontalAlignment="Left" Margin="254,130,0,0" Name="textBlock1" VerticalAlignment="Top">
        <Hyperlink Click="Hyperlink_Click">Clique Aqui</Hyperlink>
        </TextBlock>
private void Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            MainWindow m = new MainWindow();
            m.Show();
        }
<Hyperlink NavigateUri="{Binding MyUri}" 
           Command="{Binding OpenHyperlinkCommand}">Link text
</Hyperlink>
private ICommand _openHyperlinkCommand;
public ICommand OpenHyperlinkCommand {
    get
    {
        if (_openHyperlinkCommand == null) 
            _openHyperlinkCommand = new RelayCommand<object>(p => ExecuteHyperlink());
        return _openHyperlinkCommand;
    }
}

private void ExecuteHyperlink() {
    //do stuff here
}