Microsoft KB Archive/942848

= How to add Custom Styles for Windows Presentation Foundation Controls in a Windows Forms Application in C# =

Article ID: 942848

Article Last Modified on 9/21/2007

-

APPLIES TO


 * Microsoft .NET Framework 3.0

-



Source: Microsoft Support



RAPID PUBLISHING
RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.



Action
When you host Windows Prssentation Foundation (WPF) controls using ElementHost in a Windows Forms application, there is no way to specify Application level resources.



Result


Cause
A Windows Forms application uses the System.Windows.Forms.Application object rather than the System.Windows.Application object.



Resolution
Add a System.Windows.Applicaton object to the Windows Forms project which contains Application.Resources.

1. Open the Windows Forms .csproj file using notepad.

2. Add &quot;&quot; near the bottom of the file after the CSharp.targets

...









3. Add a new WPF Window class to the project called myApp.

4. Replace the code in myApp.xaml with









5. Replace the code in myApp.xaml.cs with

using System;

using System.Windows;

using System.Data;

using System.Xml;

using System.Configuration;

using System.Windows.Markup;

namespace myNamespace

{

public partial class myApp : System.Windows.Application

{

public myApp

{

InitializeComponent;

}

}

}

6. Open Program.cs

7. Insert &quot;System.Windows.Application app = new myNamespace.myApp;&quot; before &quot;Application.Run(new Form1);&quot;

[STAThread]

static void Main

{

Application.EnableVisualStyles;

Application.SetCompatibleTextRenderingDefault(false);

System.Windows.Application app = new myNamespace.myApp;

Application.Run(new Form1);

}

8. Add Custom styles to myApp.xaml as you would for any WPF applicaion and styles will be applied to WPF elements in this application as they would be in a WPF Application.



MoreInformation


<div class="summary_section">

DISCLAIMER
MICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED &quot;AS IS&quot; WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.

Keywords: kbnomt kbrapidpub KB942848

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.