barriovolvo
barriovolvo

Reputation:

WPF code-behind for resources?

I'm working on a wpf application, and up until recently, I had a ResourceDictionary inside my main window's resources part of the xaml. The resource dictionary contained an DataTemplate that was used to style several listboxes in the window. The xaml for this datatemplate contained pointers to event handlers, eg:

<Button n:Name="btnClickMe" Content="Click Me!" LeftMouseButtonUp="btnClickMe_Click" />

I recently decided to split the content of the window up into separate user controls, and to move my ResourceDictionary into it's own file. But, of course, there isn't a code-behind file for a resource dictionary file. How can I wire this up, with things split up as I've described?

Thanks in advance!

Upvotes: 3

Views: 5270

Answers (3)

Anthony Brien
Anthony Brien

Reputation: 6166

You can add a new class and name it with the same name as your resource dictionary plus the .cs extension and Visual Studio will automatically set things up so it becomes the code behind file.

For example if you have a resource dictionary called Buttons.xaml, add a file called Buttons.xaml.cs.

Upvotes: 1

Muad&#39;Dib
Muad&#39;Dib

Reputation: 29256

You should consider using RoutedCommands, I am thinking. there are many many resources online, here are a couple that might help you.

http://msdn.microsoft.com/en-us/library/ms752308.aspx http://www.devx.com/DevX/Article/37893/0/page/1

Upvotes: 0

Jeff Wain
Jeff Wain

Reputation: 1022

You can add a code-behind to a ResourceDictionary; just make sure your class names are referenced correctly. For instance, in the ResourceDictionary if you were working with AppStyles.xaml the XAML file would have a class of:

x:Class="Client.App.Shell.themes.AppStyles"

In the code-behind, AppStyles.xaml.cs, you would make sure to have the class:

namespace Client.App.Shell.themes
{
    public partial class AppStyles
    ...

Upvotes: 3

Related Questions