[ACCEPTED]-Open WPF form when clicking WPF hyperlink-hyperlink
Accepted answer
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 1 it like this:
private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
Window2 form2 = new Window2();
form2.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 8 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 7 is very problematic since once the dialogue 6 is closed it cannot be reopened, that means 5 when you click the hypelink again an exception 4 will be thrown.
Using interactivity you could 3 do this without such problems (needs a reference 2 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 1 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();
}
}
}
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?
0
And using MVVM you can do in your View
<Hyperlink NavigateUri="{Binding MyUri}"
Command="{Binding OpenHyperlinkCommand}">Link text
</Hyperlink>
and 1 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
}
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.