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
}
}
Related
I'm fiddling around with Microsofts WebView2 control. I created a simple wpf application, nothing fancy: Just MainWindow with a tabcontrol. Every tab has it's own user data directory.
Then I added a method to instantiate a tabitem and a webview and then add that control to the tabcontrol in MainWindow.
What I have so far - which is working (but...):
private async void AddTab(string serviceName, string serviceUrl)
{
TabItem tabItem = new TabItem();
tabItem.Name = serviceName;
tabItem.Header = serviceName;
myTabControl.Items.Add(tabItem);
// Create a unique user data directory for this tab
// and keep it "portable", a.k.a. in the programm directory
string userDataDirectory = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\Sessions\\" + serviceName;
// Create a new WebView2 control and pass custom user data directory
WebView2 webView = new WebView2() { CreationProperties = new CoreWebView2CreationProperties() { UserDataFolder = userDataDirectory } };
// set the source address
webView.Source = new Uri(serviceUrl);
// add webview to tab content
tabItem.Content = webView;
// Initialize the WebView2 control
await webView.EnsureCoreWebView2Async();
}
This works as intended. But as it is now, the webview content is only loaded when I click on the tab. I'm not sure how to make the tabs load in the background, so that when I click on the tab, the website is already loaded.
I found one example on stackoverflow, but it is too complex for what I want to accomplish.
My experiment with a Task brought me into the crazy world of not being able to access my thread again. Such code is way over my head (for now). I'm still learning.
I would try defining the tabitems in xaml.
The tabcontrol is an itemscontrol. I would usually bind the itemssource to a collection of viewmodels and template those out into ui. You only get whichever one of those items is selected instantiated if you bind the items like that. Defining tabitems in xaml seems to mean they're all instantiated. It's noticeably slower.
You can add other tabs afterwards in code if you wanted. But I'd check what happens with some defined in xaml first.
Another thing you could try is hidden webview2 controls in the main view. Navigate to these fixed url immediately the window renders.
Move them out the window and into your tabitem on selecting that tab, set isvisible true. Then you're just incurring rendering.
Btw
The webview2 is a wrapper around a chromium edge browser. It displays some odd behaviour like it can overflow parent containers.
I have an app which consists of a Form that after loading adds two Controls that are descended from UserControl. The UserControls are sharing the same coordinate space and I alternate which one is visible with BringToFront()/SendToBack(). Basically a poor man's WPF Pages. The UC's each have a panel that takes up the entire area of the UserControl and the panels contain all of the Buttons, TextBoxes, DataGridView's, etc.
Basically, I'm trying to make the UserControls size with the Form containing them, using either the Dock or Anchor properties, but the problem is the UserControl itself doesn't appear to have these properties, so when I resize the window, the panels containing all the content never get bigger. I've tried setting both the Anchor and Dock properties of the Panels inside each UserControl to no avail. I've also tried adding a FlowLayoutPanel to the Form, and then adding the UserControls to that instead of directly to the Form object. Same result. Did I screw myself by going with UserControls containing panels? Any way to fix this? PS, I'm kinda new to C#/.NET. Been doing most of my dev work in Java for a while now.
Code ex:
//Add the panels
FsLookupPanel = new FSLookupPanel(this, this.LdapConn, this.dbConnect);
MakeResPanel = new MakeReservationPanel(this, this.dbConnect);
this.flowLayoutPanel1.Controls.Add(FsLookupPanel);
this.flowLayoutPanel1.Controls.Add(MakeResPanel);
//this.Controls.Add(FsLookupPanel);
//this.Controls.Add(MakeResPanel);
FsLookupPanel.Visible = true;
MakeResPanel.Visible = false;
Have you tryed somthing like this ?
FsLookupPanel.Dock = MakeResPanel.Dock = DockStyle.Fill;
this.flowLayoutPanel1.Controls.Add(FsLookupPanel);
this.flowLayoutPanel1.Controls.Add(MakeResPanel);
this work with framework 3.5 so you should be alright with 4.5
You don't have it when setting the properties via the designer but the property is there. Via code it is working
Is it possible to go to next window or go back to a window using just <window> tag. I searched through internet and didn't find it anywhere. Whenever navigation is needed, I found <Window.Navigation> is in use. But using but this browser like tab on top I want window to navigate when I click m Ok or cancel button on my <Window>.
P.S. I am new to WPF. So I don't know much about it.
If NavigationWindow would work for you except for the navigation tab on top, the simplest thing to do is set ShowsNavigationUI=false. Alternatively (a bigger hammer, but more flexible) you can replace the Template on your window and that will also remove it.
It is possible to navigate by using the Window class, but it depends what you want to achieve. You can assign your newly created Windows to the applications MainWindow.
EDIT: I did some tests: Other than the documentation states you cannot assign a new Window to the MainWindow property like this:
Application.Current.MainWindow = new MyWindow(); // does not work!
The main window is special, when it is closed the application will normally be closed.
However it might be easier to use the class as it has a NavigationService property which makes navigation a easier. You finde some documentation under http://msdn.microsoft.com/en-us/library/ms750478.aspx
I have textbox inside a usercontrol and I add the usercontrol to the MainWindow with the following XAML:
<Views:MyUserControl />
I have one TextBox in MyUserControl, the problem is that the TextBox doesn't take any input. Backspace och space works, but if I press the letter och numbers no text is added to the TextBox.
I have made sure that the text is not just hidden in the TextBox.
I have also tried to add a RichTextBox to MyUserControl with the same result, it doens't take any input (beside space och backspace).
I have also tried to add a TextBox to the MainWindow with the same result; it doens't take any input (beside space och backspace).
Also MyUserControl is added in a TabControl and TabItem.
Any clues?
Edit: Additional information
Forgot to write that I'm opening/creating the WPF Window from a WinForm application.
When I set my startup project in VS10 to be my WPF-project it work great with the keyboard input to the TextBox.
How come?
Im opening/creating my WPF windows with the following code:
MyWpfProject.MainWindow mw = new MyWpfProject.MainWindow();
mw.Show();
Edit: Solution
So I guess my real problem was that is was opening the WPf project from a WinForms application.
I added the following code:
MyWpfProject.MainWindow mw = new MyWpfProject.MainWindow();
ElementHost.EnableModelessKeyboardInterop(mw);
mw.Show();
"The EnableModelessKeyboardInterop() call is necessary to handle keyboard input in the WPF window if loaded from a non-WPF host like WinForms."
http://weblogs.asp.net/jdanforth/archive/2008/07/29/open-a-wpf-window-from-winforms.aspx
Answer to my own question (if someone else run into the same problem):
If you open a WPF-form from a WinForms application you have to do the following to get keyboard input:
MyWpfProject.MainWindow mw = new MyWpfProject.MainWindow();
ElementHost.EnableModelessKeyboardInterop(mw);
mw.Show();
"The EnableModelessKeyboardInterop() call is necessary to handle keyboard input in the WPF window if loaded from a non-WPF host like WinForms." http://weblogs.asp.net/jdanforth/archive/2008/07/29/open-a-wpf-window-from-winforms.aspx
maybe your user control is getting the keyboard event instead of your textbox? try to search in this way, it happens with mouse buttons.
I had the same problem but my Environment was built such that I could not apply the solution above. As it turned out that wasn't the problem. It was much more easier than expected.
I host a WPF User Control inside WinForms. My WinForms MainView (Startup form) did override the "ProcessCmdKey". Because of logical errors it returned nothing.
Nevertheless I received all key events in code behind in my XAML file but this events never updated the "Text" of my text box. Only space and backwards did so and this were the key events I didn't receive in code behind.
So in case you don't think of the simplest solution here it is for VB.NET.
STRG+F "ProcessCmdKey" and make sure that you Return the correct value of you may completely uncomment it just to verify that it isn't causing you the same trouble as it did to me.
I would like to add my own answer just on the off chance it can help anyone else with the same or similar problems. My particular use case was a .Net windows form created by an addin. The addin was running out of Autodesk Inventor which I think may be WPF (but don't take my word for it.) The addin had several windows forms some docked within the application and with some acting as popups or a separate secondary window. The solution was to set the windows forms owner to one of the docked forms. Doing this allowed text to be edited and entered into its textboxes. Very niche solution to a niche problem but hopefully it may help someone at some point in the future.
private void CreatePanels(DockableWindows DockableWindows)
{
//Create the dockable window that the panel form will live on.
DockableWindow SideWindow =
DockableWindows.Add(SidePanelNames.GUID, SidePanelNames.InternalWindowName, SidePanelNames.Title);
//Configure the windows settings.
SideWindow.ShowTitleBar = true;
SideWindow.ShowVisibilityCheckBox = true;
SideWindow.DisableCloseButton = false;
SideWindow.Visible = true;
SideWindow.AddChild(SidePanel.Handle); //Add the forms as children and then display to the user.
SidePanel.Show(); //Display the panel to the user.
}
/// <summary>
/// Creates the Display window were order and patient information is displayed to the user.
/// This method makes the adjust device form the owner of the display window. Because of this
/// dependecency it should be run after the panels have been created.
/// </summary>
private void CreateDisplayWindow()
{
DisplayForm = new DisplayForm //Create the display form.
{
Owner = SidePanel //Doing this allows text boxes to have values entered into them.
};
DisplayForm.Show(); //Show the display form to the user.
}
//Adding the side side panel as the owner of the display form was effectively the only change I needed to make.
I will add a few words of warning for anyone attempting this solution. Simply put I've since found out it's not great. It prevents the form or any of its controls from detecting certain key presses (enter and tab) which matters a lot for my usage. You'll find a much better and solution here https://forums.autodesk.com/t5/inventor-forum/dockable-window-with-wpf-controls-don-t-receive-keyboard-input/td-p/9115997
I have a complex Windows Forms GUI program that has a lot of automated control generation and manipulation. One thing that I need to be able to do is add a custom UserControl to a newly instatiated TabPage. However, when my code does this I get automatic resizing events that cause the formatting to get ugly. Without detailing all of the different Containers that could possibly be involved, the basic issue is this:
At a certain point in the code I create a new tab page:
TabPage tempTabPage = new TabPage("A New Tab Page");
Then I set it to a certain size that I want it to maintain:
tempTabPage.Width = 1008;
tempTabPage.Height = 621;
Then I add it to a TabControl:
tabControl.TabPages.Add(tempTabPage);
Then I create a user control that I want to appear in the newly added TabPage:
CustomView customView = new CustomView("A new custom control");
Here is where the problem comes in. At this point both the tempTabPage and the customView are the same size with no padding or margin and they are the size I want them to be. I now try to add this new custom UserControl to the tab page like this:
tempTabPage.Controls.Add(customView);
When making this call the customView and it's children controls get resized to be larger and so parts of the customView are hidden.
Can anyone give me any direction on what to look for or what could be causing this kind of issue?
Thanks ahead of time.
The UserControl's "AutoScaleMode" property should be set to "None".
If you want the customView to fill the TabPage.
Use Dock like this:
tempTabPage.Controls.Add(customView);
customView.Dock = DockStyle.Fill;
Then the customView will fill out the space in the TabPage, but you have to handle resizing of the customView so child controls will be shown properly.
I had the same issue.
The UserControl's "AutoScaleMode" set to "None" works for me.