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

发布于 2024-11-02 06:49:48 字数 73 浏览 1 评论 0原文

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

I want to open a new WPF form when I click in a WPF hyperlink. I've seen a lot of examples that only opens web url, but I want to open a new WPF form.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

桃酥萝莉 2024-11-09 06:49:48

您可以像这样实现:

<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();
}

You can achive this like this:

<Label Height="25" Margin="26,27,116,0" Name="label1" VerticalAlignment="Top">
    <Hyperlink Click="Hyperlink_Click">Click Me</Hyperlink>
</Label>

and handle it like this:

private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
    Window2 form2 = new Window2();
    form2.Show();
}
心房敞 2024-11-09 06:49:48

您可以只处理单击事件:

<Hyperlink Click="Hyperlink_Click">Link</Hyperlink>
private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
    Dialogue diag = new Dialogue();
    diag.Show();
}

您也可以使用 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>

然而,这是非常有问题的,因为一旦对话框关闭,它就无法重新打开,这意味着当您再次单击超链接时,将引发异常。


使用交互性,您可以做到这一点而不会出现此类问题(不过需要参考 Blend SDK):

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<Hyperlink>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <t:CreateDialogAction Type="{x:Type local:Dialogue}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <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();
        }
    }
}

You could just handle the click event:

<Hyperlink Click="Hyperlink_Click">Link</Hyperlink>
private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
    Dialogue diag = new Dialogue();
    diag.Show();
}

You could also go crazy with 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>

This however is very problematic since once the dialogue is closed it cannot be reopened, that means when you click the hypelink again an exception will be thrown.


Using interactivity you could do this without such problems (needs a reference to the Blend SDK though):

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<Hyperlink>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <t:CreateDialogAction Type="{x:Type local:Dialogue}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Hyperlink.Inlines>
        <Run Text="Open Dialogue"/>
    </Hyperlink.Inlines>
</Hyperlink>

The action for this:

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();
        }
    }
}
心凉怎暖 2024-11-09 06:49:48

使用 MVVM,您可以在 View

<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
}

And using MVVM you can do in your View

<Hyperlink NavigateUri="{Binding MyUri}" 
           Command="{Binding OpenHyperlinkCommand}">Link text
</Hyperlink>

and in your 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
}
铃予 2024-11-09 06:49:48

XAML:

<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();
        }

这个?

XAML :

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

CodeBehind :

private void Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            MainWindow m = new MainWindow();
            m.Show();
        }

this?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文