bind method on click event of "LabelLink" control in WPF - Caliburn
Hi I have problem to bind method on click 开发者_Go百科event of "LabelLink" control. I use pseudeo LabelLink control, I think everybody know this solution with textBox and hyperlink.
Here is my code:
<TextBlock Margin="10,12,10,4">
<Hyperlink Name="RegLink"
NavigateUri="http://registracia.azet.sk/"
Micro:Message.Attach="[Event Click]=[Action OpenDefaultBrowser(NavigateUri)]"
FontSize="12">Registrácia</Hyperlink>
Problem is that I can bind method only on framework element.
I get this compile error:
Cannot attach type "ActionMessage" to type "Hyperlink". Instances of type "ActionMessage" can only be attached to objects of type "FrameworkElement".
I search with google, bud didn’t find any suitable solution.
Thank for advice.
I try make a fake linkLabel with textBlock or Label control but they don’t have click event handler.
I have adapted @Rick's answer to Caliburn.Micro
In TestView.xaml
<Grid>
<Grid.Resources>
<Style x:Key="HyperlinkButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Button Style="{StaticResource HyperlinkButton}" Focusable="False"
cal:Message.Attach="[Action NavigateTo('http://www.stackoverflow.org')]">
<TextBlock>
<Hyperlink Focusable="False">www.stackoverflow.com</Hyperlink>
</TextBlock>
</Button>
</Grid>
In TestViewModel.cs
public void NavigateTo(string url)
{
Process.Start(new ProcessStartInfo(url));
}
I am using this way in my application and it works. Hope it helps you.
You can embed your Hyperlink
in a Button
that doesn't look like a button in order to get a Click
event that occurs on a FrameworkElement
:
<Grid>
<Grid.Resources>
<Style x:Key="HyperlinkButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<StackPanel>
<Button Name="button1" Style="{StaticResource HyperlinkButton}" Click="Button_Click" Focusable="False">
<TextBlock>
<Hyperlink NavigateUri="http://www.stackoverflow.com" Focusable="False">
StackOverflow
</Hyperlink>
</TextBlock>
</Button>
</StackPanel>
</Grid>
Perhaps you can adapt this technique to Caliburn.Micro.
I Did this in vs2017
Method In ViewModel
public void LinkClick(string url)
{
if (!string.IsNullOrEmpty(url))
{
System.Diagnostics.Process.Start(new ProcessStartInfo(url));
}
}
Then as above in the View XAML namespace declaration at the top
xmlns:cal="http://www.caliburnproject.org"
<Grid>
<Grid.Resources>
<Style x:Key="HyperlinkButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<!-- Collapsed so you can not see it ! -->
<TextBlock x:Name="LinkTarget" Text="http://www.google.com" Visibility="Collapsed"/>
<!-- Bound the TAG to the Collapsed TextBlock Text then pass that into the method in VM -->
<Button Tag="{Binding ElementName=LinkTarget, Path=Text}" Style="{StaticResource HyperlinkButton}" Focusable="False"
cal:Message.Attach="[LinkClick($this.Tag)]">
<TextBlock>
<Hyperlink Focusable="False">Read More ...</Hyperlink>
</TextBlock>
</Button>
</Grid>
Hope this helps some one else! I know its an old post but, I landed here!
精彩评论