I have a C# application which contains 2 project, and when I set the first project which contains a Styled WPF Form as startup project, it has it's own style and everything is fine. But when I use this code to show that WPF from from a Windows Form Application, the first project's form loses its own style:
Window introForm;
introForm = new Client.MainWindow();
introForm.Show();
I have no idea why it happens
I have found my answer here:
https://stackoverflow.com/a/6042515/7147513
this fixed my problem:
var foo = new Uri("pack://application:,,,/MyProject;component/Resources/Styles.xaml", UriKind.RelativeOrAbsolute);
Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = foo });
Related
I am developing a C# Windows Form project with Visual Studio 2019. I have inherited a new control, called NewGroupBox, from System.Windows.Forms.GroupBox, the control is under name space MyProject.NewCtrls and was added through the standard procedure How to: Inherit from Existing Windows Forms Controls. I did not do any change on the code generated except changing public partial class NewGroupBox: Control to public partial class NewGroupBox: GroupBox
In the main frame, I have a GroupBox object called groupbox_Table.
groupBox_Table = new System.Windows.Forms.GroupBox();
The form designer could open normal. However, if I manually changed GroupBox with NewGroupBox
groupBox_Table = new MyProject.NewCtrls.NewGroupBox();
The form designer cannot be opened properly. The error message shows "Could not find type 'MyProject.NewCtrls.NewGroupBox' ...". Of course the type is defined in my project and there is no error when building the project.
How do I fix this problem so that form designer can open normally? Thanks a lot!
I am showing a WPF exe window inside another WPF application using winform host.
I have created a panel in main application and set it as child of winformhost.
mHostingPanel = new System.Windows.Forms.Panel()
{
BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
};
mWinformHost = new WindowsFormsHost();
mWinformHost.Child = mHostingPanel;
and then I start the other window process and set hosting panel as parent.
WindowsAPI.SetParent(mProcess.MainWindowHandle, mHostingPanel.Handle);
My question is if I launch the application,Who will be rendering my Child WPF window whose parent is a winform panel.Will it be Direct-X or GDI context of Panel?
Also if I set Allowtransparency=True on child WPF application,The UI doesnt show up in hosting panel.
Found the reason.Its called airspace issue (when win32 and WPF trying to share pixels) and I dont think it can be solved by any framework API as microsoft denied it.
https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2644120-bring-back-the-hwndhost-isredirected-and-compositi
Mitigating AirSpace issues
To preface this question, I am working on coding the back end of an application whose UI was put together by someone else (I believe using Blend). The application consists of a series of "Screens," whose root element in XAML is "UserControl". There is no use of the "Window" tag anywhere in the source.
What I want to do is remove the Windows border that is added to the outside edge of the application when I run the program. The border currently consists of forward/backward buttons like a web browser, and an X button to close.
All I can find from searches are instructions to add
WindowStyle="None"
to the
<Window>
element. But of course, I don't have one of those, and WindowStyle is not a property of UserControl. Anyone know how to accomplish this with UserControl root elements?
Edit: The StartupUri for the application is
this.StartupUri = new Uri(#"pack://application:,,,/WpfPrototype1.Screens;Component/Screen_1.xaml");
the file it points to does not have a Window tag.
Based on the comments above it seems your MainWindow is created dynamically somewhere, however you can use the Application class to get the applications MainWindow.
var mainWindow = Application.Current.MainWindow;
And you can then set your border style from there
Example:
private void RemoveBorder()
{
var mainWindow = Application.Current.MainWindow;
if (mainWindow != null)//should never be
{
mainWindow.WindowStyle = System.Windows.WindowStyle.None; // removes top bar (icon, title, close buttons etc)
mainWindow.AllowsTransparency = true; //removes the border around the outside
}
}
I have a windows form which calls other windows form that work within my application. What I want to accomplish is to get away from this whole "windows form" thing and use WPF View (usercontrol) instead. Is there a way I can call a view to show it from my form?
ElementHost host = new ElementHost();
Cars.WPF.Views.DescriptionView descView = new Cars.WPF.Views.DescriptionView();
host.Controls.Add(descView);
host.Dock = DockStyle.Fill;
I get error: --> Argument 1: cannot convert from 'Car.WPF.Views.DescriptionView' to 'System.Windows.Forms.Control'
Add a panel in your winform (lets say panel1)
Define ElementHost at class level, Also define WPF Control at class level
ElementHost host;
Cars.WPF.Views.DescriptionView descView;
In form load event do:
host= new ElementHost();
panel1.Controls.Add(ctrlHost); //Add Element host to panel1
descView = new Cars.WPF.Views.DescriptionView();
descView.InitializeComponent();
host.Child = descView; //Instead of adding WPF control to Winform do this
Also in your project references add:
PresentationCore
PresentationFramework
WindowsBase
Yes, use Element Host.
http://www.switchonthecode.com/tutorials/wpf-tutorial-using-wpf-in-winforms
http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.elementhost.aspx
This question already has answers here:
How to programmatically create a WPF window in a WinForm application
(2 answers)
Closed 5 years ago.
I added to my WindowsForm app a new WPF window called novoLogin.
After adding it, I added the system.xaml reference....debug fine.
Now I'm trying to open this new window from the existing windowsForm.
novoLogin nl = new novoLogin();
nl.show();
The compiler is giving this error:
Error 1 'WindowsFormsApplication1.novoLogin' does not contain a
definition for 'show' and no extension method 'show' accepting a first
argument of type 'WindowsFormsApplication1.novoLogin' could be found
(are you missing a using directive or an assembly reference?)
This brief article explains how you can achieve this.
If you find yourself in need to open a WPF Window from a WinForms program, this is one way to do it (works for me):
Create/Add a new project of type WPF Custom Control Library
Add a new Item of type Window (WPF)
Do your thing with the WPF Window
From your WinForms app, create and open the WPF Window
using System;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
var wpfwindow = new WPFWindow.Window1();
ElementHost.EnableModelessKeyboardInterop(wpfwindow);
wpfwindow.Show();
Have a look to this: http://www.mobilemotion.eu/?p=1537&lang=en
Summary:
Open the project’s manifest file (the one with the .csproj or .vbproj extension) in any text editor.
The top node usually contains several tags, one for each build configuration and a global one. In the global
node (the one without Condition attribute), search for
the sub-node or create one if it does not
exist. This node should contain two GUIDs:
FAE04EC0-301F-11D3-BF4B-00C04F79EFBC, which stands for a C# project,
and 60dc8134-eba5-43b8-bcc9-bb4bc16c2548 which stands for WPF. The
full line should look as follows:
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
(If you’re interested in details, codeproject holds a complete list of
potential project GUIDs:
http://www.codeproject.com/Reference/720512/List-of-Visual-Studio-Project-Type-GUIDs)
Reload the project in Visual Studio, and open the Add New Item wizard.
Since the project is now officially classified as WPF project, this
wizard should now contain the WPF window option. By the way, since
there is no WinForms project GUID that could be overwritten, this
approach does not harm the existing project components.
I just tried this approach for a VB.NET project and it works!
Using VB.NET obviously you have to edit above lines substituting the GUID from {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} to {F184B08F-C81C-45F6-A57F-5ABD9991F28F}
I wanted to show wpf form in windowForm and there was some resource problem...
(because I used resources..). Finally I used this code in my windowsForm project:
First create a global instance of your app class like this :
WPFTest.App app;
why this is global?
because this class is singleton and you can not create more than one instance in the same AppDomain
Now for example you have a button event to show wpf form. At the button event we have :
private void button1_Click(object sender, EventArgs e)
{
if (System.Windows.Application.Current == null)
{
app = new WPFTest.App()
{
ShutdownMode = ShutdownMode.OnExplicitShutdown
};
app.InitializeComponent();
}
else
{
app = (WPFTest.App)System.Windows.Application.Current;
app.MainWindow = new WPFTest.YourWindow();
System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(app.MainWindow);
app.MainWindow.Show();
}
}
note : WPFTest is name of your project and YourWindow() is window that you wanna to show