5

I would like to be able to easily navigate from the XAML code:

  <Binding Path="Duration">

to the corresponding C# code for the property:

  public static DependencyProperty DurationProperty =
     DependencyProperty.Register("Duration", typeof(int), typeof(MainWindow));

The closest I seem to be able to do is to go to the file containing the code and then I have to perform a text search. Seems like there ought to be a better way.

The answer is probably already out there but I haven't gotten the correct combination of keywords to find it yet.

Harold Bamford
  • 1,589
  • 1
  • 14
  • 26
  • I just press F12 and it takes me to declaration but I think that is a ReSharper command. If you define your datacontext in your XAML Right-Click> Go To Definition should take you there (not sure what is the keyboard shortcut for that VS command). Did you try that? – Dean Kuga May 25 '11 at 18:06
  • Even with a DataContext, right click on "Duration" does not yield a "Go To Definition" option. You must be getting that option from ReSharper. – Harold Bamford May 25 '11 at 18:19

3 Answers3

3

I do not see any way to do it since binding is not strongly typed reference, it is just name of the property to use and nobody except you knows which class defines this property. Though with Resharper for example you can use navigate to member (ctrl+alt+shift+N in IntelliJ schema) and search there for Duration, but this will give you all the classes in your solution which define Duration member.

I doubt there is anything production ready in this area though somebody might write some specific addin for Visual Studio to handle some cases.

Snowbear
  • 16,924
  • 3
  • 43
  • 67
1

Simply place your mouse cursor on the method name & hit F12.

It will redirect you to the method defined in the cs file.

Don Chakkappan
  • 7,397
  • 5
  • 44
  • 59
0

Well, it seems that there isn't a standard (free) way to do this. But as others have pointed out, it is just a string with quotes. So I wrote a little macro in VB and stuck it into the XAML code editor's context menu using the technique found in VS2010 Macro/Add In for "Run" and "Run On" commands in Visual Studio 2010

Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

' This is intended to be used as a quick & dirty "go to definition" for
' properties referenced in XAML. The idea is that you have something like:
'
'  <Binding Path="InterestRate">
'
' You click on the name and run the macro which looks for the current word
' in quotes ("InterestRate") which should match on something like:
'
' MainWindow.xaml.cs(41):  DependencyProperty.Register("InterestRate", ...
'
Public Module QuotedSearch
    Sub DoQuotedSearch()
        Dim Pattern As String
        DTE.ExecuteCommand("Edit.SelectCurrentWord")
        Pattern = """" & DTE.ActiveDocument.Selection.Text & """"
        DTE.ExecuteCommand("Edit.FindinFiles")
        DTE.Find.FindWhat = Pattern
        DTE.Find.Target = vsFindTarget.vsFindTargetFiles
        DTE.Find.MatchCase = True
        DTE.Find.MatchWholeWord = False
        DTE.Find.MatchInHiddenText = True
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.SearchPath = "Current Project"
        DTE.Find.SearchSubfolders = True
        DTE.Find.FilesOfType = "*.*"
        DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResults1
        DTE.Find.Action = vsFindAction.vsFindActionFindAll
        If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
            Throw New System.Exception("vsFindResultNotFound")
        End If
    End Sub
End Module

However, I'm still looking for something more precise.

Community
  • 1
  • 1
Harold Bamford
  • 1,589
  • 1
  • 14
  • 26