0

So I've got a hyperlink that I have hooked up to the code behind like so:

Xaml

<TextBlock x:Name="Hyperlink" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="3" FontSize="14" Foreground="White">           
      <Hyperlink NavigateUri="{Binding StreetViewString}" RequestNavigate="Hyperlink_RequestNavigate" Foreground="White" StylusDown="Hyperlink_StylusDown" TouchDown="Hyperlink_TouchDown">
             Google
      </Hyperlink>
</TextBlock> 

Code Behind

private void addToDiary_MouseDown(object sender, MouseButtonEventArgs e)
{
    ((sender as Button).DataContext as MyViewModel).MyCommand.Execute(null);
    e.Handled = true;
}

But I would like to hook this straight up to the command it is executing

    private ICommand _myCommand;
    public ICommand MyCommand
    {
        get
        {
            return _myCommand
                ?? (_myCommand= CommandFactory.CreateCommand(
                () =>
                {
                    DoSomething();
                }));
        }
    }

but the only thing that is stopping me is that I cannot set e.Handled = true from my command. Is it possible to do this without using the code behind?

JKennedy
  • 18,150
  • 17
  • 114
  • 198
  • If you had ICommand which if you don't use any framework like prism or mvvm light, does not come out of the box. Then you can pass your MouseButtonEventArgs as a parameter in Execute, and then you can do what you want with it. – adminSoftDK May 06 '15 at 08:06
  • @adminSoftDK I could implement `ICommand` but my next question would be could I do this so that I dont need the code behind. I.e. send the `MouseButtomEventArgs` straight from the Xaml? – JKennedy May 06 '15 at 08:10
  • I don't know anyway of doing it without the code behind. I am doing something similar on application close. Maybe somebody else will help with this question :) – adminSoftDK May 06 '15 at 08:14
  • MVVM != no codebehind, and performing UI logic in the VM (killing event bubbling, for example) != MVVM. –  May 06 '15 at 14:07
  • MVVM can have 'code-behind' but only for View concerns, not for accessing the DataContext aka the ViewModel. One might say, 'if you're doing MVVM properly you don't have code-behind files, only attached behaviors, which are code-behind files :)' – AwkwardCoder May 06 '15 at 14:34

1 Answers1

0

I am going to presume you're using MVVM and ideally you want to be able to 'handle' the click of the hyperlink in the ViewModel and not in the View - which is the correct way to do this.

In which you are probably best using a normal WPF button which has been styled to look like a hyperlink and then bind in the Command property of the button to an instance of your ICommand implmentation.

The following should style the button:

<Style x:Key="HyperLinkButton"
       TargetType="Button">
    <Setter
        Property="Template">
        <Setter.Value>
            <ControlTemplate
                TargetType="Button">
                <TextBlock
                    TextDecorations="Underline">
                <ContentPresenter /></TextBlock>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter
        Property="Foreground"
        Value="Blue" />
    <Style.Triggers>
        <Trigger
            Property="IsMouseOver"
            Value="true">
            <Setter
                Property="Foreground"
                Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>

Applying the Style and binding the Command property, this requires you have bound the DataContext of the containing control\view to the ViewModel. as I said if you are doing MVVM this should be obvious:

<Button Style="{StaticResource HyperLinkButton}"
        Content="Some Link"
        Command={Binding Path=MyCommand, Mode=OneWay/>
AwkwardCoder
  • 24,893
  • 27
  • 82
  • 152
  • Thanks for the answer, though it doesn't really answer my question. there is a `command` property I can use on the `Hyperlink` object. The only reason I currently haven't done this is because I cannot set the `EventArgs` to handled in the command and was wondering if there was a way to do this – JKennedy May 06 '15 at 08:54
  • yes I am. This is why I would like to remove the `e.Handled` from the code behind – JKennedy May 06 '15 at 10:09
  • so not the solution I provide, it completes keeps the UI concerns out of the ViewModel. – AwkwardCoder May 06 '15 at 10:13
  • Oh yes I see what you mean now because the button autoamtically sets e.handled so the event doesnt bubble up. Thanks – JKennedy May 06 '15 at 10:15
  • 1
    Exactly - the user thinks it's a hyperlink button, but in reality it is a normal WPF button and it has the standard button behavior you want - as in the event does not bubble up – AwkwardCoder May 06 '15 at 10:18
  • it may be worth adding that explanation to the answer to clarify that this answers the question – JKennedy May 06 '15 at 10:31