Share via


How to: Rename a Symbol that is Declared in XAML

You can rename a symbol that is declared in XAML by using the WPF Designer for Visual Studio. You can also change the x:Class definition of a class that is declared in XAML.

Note

Visual Basic does not have a default refactor mechanism similar to Visual C#.

To rename a symbol declared in XAML

  1. Create a new WPF Application project in Visual C# named WPFApplication1. For more information, see How to: Create a New WPF Application Project.

    MainWindow.xaml opens in the WPF Designer.

  2. In XAML view, replace the default <Grid> element with the following markup.

    <Grid x:Name="grid1">
    </Grid>
    
  3. In XAML view, right-click and select View Code from the context menu.

    The Code Editor opens the code-behind file.

  4. In the Code Editor, insert the following line in the MainWindow class constructor.

    this.grid1.AllowDrop = true;
    
  5. Right-click grid1, point to Refactor, and select Rename from the context menu.

    The Rename dialog box opens.

  6. Type _grid in the New name: text box and click OK.

    The Preview Changes - Rename dialog box opens.

  7. Click Apply.

    The symbol grid1 is renamed to _grid.

  8. Right-click _grid and select Go To Definition from the context menu.

    XAML view opens with the cursor in the <Grid> element named _grid.

To change the x:Class definition

  1. Add a WPF User Control item to the project. Name the control UserControl1.xaml. For more information, see How to: Add New Items to a WPF Project.

  2. In XAML view, right-click and select View Code from the context menu.

    The Code Editor opens the code-behind file.

  3. Highlight the UserControl1 class name and right-click. In the context menu, point to Refactor, and select Rename.

    The Rename dialog box opens.

  4. Type MediaElement in the New name: text box and click OK.

    The Preview Changes - Rename dialog box opens.

  5. Click Apply.

  6. Right-click the MediaElement class name and select Go To Definition from the context menu.

    The Find Symbol Results window appears and lists two matches.

  7. In the Find Symbol Results window, double-click the match for the UserControl1.xaml file.

  8. XAML view opens with the caret in the <UserControl> element. The x:Class assignment reads x:Class="WpfApplication1.MediaElement".

See Also

Reference

Rename Refactoring (C#)

Refactoring and Rename Dialog Box (Visual Basic)