user16364377
user16364377

Reputation:

How to rename WPF window file in Visual Studio?

Is it possible to rename WPF Window in Visual Studio Community? Doing it breaks entire project, because it seems old name is used in some other places, and its very hard to track them down. The only method I'm aware of is removing old window, and creating new one, while moving code from previous window. But maybe there is better way to do it?

Upvotes: 0

Views: 2200

Answers (3)

RudolfJan
RudolfJan

Reputation: 91

The best strategy is to select the xaml file in the solutions folder. Rename it there. This will rename both the xaml and xaml.cs file. It also will rename the class in the code behind

It will NOT rename the class in the xaml file. This is the very first line in your xaml file. You need to rename this manually.

You may need to adjust the namespace as wel for consistency. This may trigger other error messages.

Sometimes this behaves a bit nasty, because the compiler will generate code for the xaml file. If it does not compile and you get a lot of weird errors, do a rebuild for your project.

Upvotes: 0

lidqy
lidqy

Reputation: 2463

You must ensure that in both relevant files Window1.xaml and Window1.xaml.cs the partial Window1 class is renamed equally. Also you should Clear the build (In VS-Menu 'Build'-> 'Clear Build') and/or manually delete the "Obj" folder of the project. There are generated code files like Application.g.cs etc in the Obj folders that might refer to old entities, and then causes these issues.

Upvotes: 0

Michał Turczyn
Michał Turczyn

Reputation: 37500

It breaks, because, most probably, you aren't renaming also code-behind class.

To do so, you need to rename class in Window.xaml.cs.

So, right click on that class (should be Window) and rename appropriately. Also, rename the file as well.

Also, make sure that in XAML you refer to correct class after renaming.

So to break it down:

  1. Rename XAML file.
  2. Make sure that code-behind file and class are renamed appropriately.
  3. Make sure to apply that change in XAML file, where you specify x:Class attribute, eg. x:Class="WpfTestApp.MainWindow"

Upvotes: 1

Related Questions