I am trying to have the SizeToContent="WidthAndHeight" and WindowState = "Maximized" at the same time. I tried to change the state of my window during loading, as well as in the Constructor of the MainWindow, but no luck.
<Window x:Name="MainWindowMy" x:Class="ManyTabControls.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Pressure Vessel Design" WindowState="Maximized" SizeToContent="WidthAndHeight" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" Loaded="MainWindowMy_Loaded" >
<Window.Resources>
I have got a TabControl with a lot of tabs, in the design viewer, tabs are flowing down into different rows. And when I change the width of the TabControl so that the tabs be in the same line, then they go outside the boundary of the MainWindow. And if I set the width of the Mainwindow, then the MainWindow would have a constant width. That means if the size of monitor gets smaller, then part of the MainWindow that is bigger than the screen will not be displayed. I hope I made myself clear.
Thanks Eugene, I got the idea. I already tried to change the state of my MainWindow at run time, but it didn't work out (MainWindow didn't occupy the full screen).
The reason, as Eugene mentioned, is that these properties conflict with each other (SizeToContent gets the priority) so I have to turn one off in order to be able to turn the other on. Hence, to solve the problem:
private void MainWindowMy_Loaded(object sender, RoutedEventArgs e)
{
this.SizeToContent = System.Windows.SizeToContent.Manual;
this.WindowState = System.Windows.WindowState.Maximized;
}
It's not the most elegant way of doing it, but it serves the purpose for now. However, if anybody could come up with a more elegant solution, it would be greatly appreciated.
Related
I experienced what appears to be a very obvious memory leak in an application that would open a settings window containing a TabControl with a number of TabItems. Initially believing that one of the user controls shown must be the culprit I commented out a bunch of things, finally getting out JetBrains dotMemory and making a demo-program.
The issue (I think)
When a Window contains a TabControl with at least one TabItem, when the window is closed, the Window-object still exists. If there are no TabItems in the TabControl, the Window-object is destroyed immediately (as expected).
Retention
According to dotMemory the 'Retention' is from WindowAutomationPeer(._owner), from TabControlAutomationPeer(._parent), from TabItemAutomationPeer(._parent), from ElementProxy(._peer) and then it says "RefCounted handle" at the bottom.
Reproduction
Create a new C# WPF application (Target framework: .NET Framework 4.7.2), named "TabsInWindows"
Add a button to MainWindow:
<Window x:Class="TabsInWindows.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300">
<Grid>
<Button Content="Open tab window" Click="Button_Click"/>
</Grid>
</Window>
Create a new Window, "TabsWindow" with a TabControl and a TabItem:
<Window x:Class="TabsInWindows.TabsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="TabsWindow" Height="200" Width="300">
<Grid>
<TabControl x:Name="Subject">
<TabItem></TabItem>
</TabControl>
</Grid>
</Window>
Make the button in MainWindow open a new TabWindow
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//TabsWindow.Open(this);
TabsWindow w = new TabsWindow();
w.Show();
}
}
Start the application. Every time you press the button a new window is created, but the TabItem (?) and therefore the TabWindow remains when the window is closed. (Repeat any number of times)
How to fix?
My issue is that in my actual application all contents of all tabs appears to be retained in memory, causing a significant memory leak.
I have tried, in the demo application, to do a number of things to avoid the hanging objects; Setting content of Grid to null. Clearing the Items in TabControl (Subject). Clearing the Children of the Grid.
None of it has worked.
I cannot work out what the 'AutomationPeer'-objects are or what the ElementProxy is created by and why it won't die.
If anyone can tell me how to get around this issue, or can shed some light on what ElementProxy is and why it is hanging around, it would be most helpful.
While writing this, I did keep dotMemory running with the test app and a while after having done anything last, the objects did appear to have been removed....
Which then raises the question: How long can I expect an object to be visible in memory, with references, before it is removed?
In an actual project
I then tried something similar in an actual project, ensuring that non of our own controls were directly linked to SettingsWindow (I'm not ruling out that I have a memory/reference issue in one of our controls, so any control listed directly in "Key Retention Paths" have been commented out).
I am left with "3 unique branches", one being an 'EffectiveValueEntry[40]' from out own extension of a ListBox, the other two are 'EffectiveValueEntry' ([19] and [22] respectively), both from a TextBlock, from TextBlockAutomationPeer[4], List, ListBoxItemAutomationPeer, ElementProxy.
After about ten minutes of doing nothing, the SettingsWindow was still there, but the "Key Retention Paths" has changed, and the "20 unique branches" are all EffectiveValueEntry ([32] on the first, [42] on the rest), TextBox, TextEditor, but now "F-Reachable Queue" is in the bottom of the list.
After about ten minutes more, the SettingsWindow was finally gone.
I then opened the settings window a few times again, and a minute after closing the last, only the 'TextBox'-references where left and a forced Garbage Collection later (using the button in dotMemory), the object references are gone.
What to believe?
So apparently, if I wait long enough 'magic' will happen - but this is a computer - not a magic-box!
Can anyone enlighten me on why some objects will appear in memory longer, but eventually be removed? How long should I expect such objects to lay about?
I would also like a way to prevent these 'ghost' objects from the TabItems, there should be no reason for them to take up memory if they will eventually be removed anyways...
You see, I discovered this while doing performance testing of some UI components in the SettingsWindow, and repeated tests took longer and longer as more memory was used, so simply waiting for the references to go away is not a very good option.
And if you are unable to help; thank you for taking the time to read my wall of text...
Set the owner of the TabWindow's
private void Button_Click(object sender, RoutedEventArgs e)
{
TabsWindow w = new TabsWindow();
w.Owner = this;
w.Show();
}
I've built a dialog window that i plan to use in my whole application instead of using a message box. Whenever i need to use it, i would call it in the code behind of my Window i'm currently working with this syntax:
public void ShowDialogWindow(object sender, DialogEventArgs e)
{
DialogWindow dialog = new DialogWindow(e.MessageToShow, DialogType.Error, ButtonsType.OkOnly, this.ActualWidth, this.ActualHeight, this);
dialog.ShowDialog();
}
this is the constructor of my Dialog Window
public DialogWindow(string messageToDisplay, DialogType dialog, ButtonsType buttons, double width, double height, object Owner)
{
InitializeComponent();
this.DataContext = this;
this.Owner = Owner as Window;
AWidth = width;
AHeight = height;
-----
}
and this is the opening Window tag in the xaml
<Window x:Class="DialogWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
---
mc:Ignorable="d" WindowStyle="None"
WindowStartupLocation="CenterOwner"
AllowsTransparency="True"
Width="{Binding AWidth}" Height="{Binding AHeight}"
MinHeight="720" MinWidth="1080">
Now my problem is. When i call this dialog when the owner is Minimized (having set MinWidth = 1080 and MinHeght = 720) the dialog "kinda" fits (ActualWidth and Actual Height of both Window and DialogWindow are the same, but visually the DialogWindow seems a little bit bigger than the Owner)
But when i go full screen it happens this:
Not only the ActualHeight is different to the property AHeight (which is set correctly to the ActualHeight of the Owner), but also is not centered on the Owner window at all, but overflow on my second screen. What cause this, and how can i solve it?
So i'm going to post what it solved my problem, but i have absolutely no idea why that happened, so if anyone has a better answer i will check that as accepted.
To solve my problems i removed the binding for width and height and simply added in the constructor of the DialogWindow
this.Width = OwnerActualWidth;
this.Height = OwnerActualHeight;
since i am passing those parameter in input
I have custom ContentControl
public class MyContentControl: ContentControl
{....}
with Content defined in XAML like this
<controls:MyContentControl x:Name="myContentControl">
<controls:MyContentControl.Content>
<controls:UserControl1 />
</controls:MyContentControl.Content>
</controls:MyContentControl>
Content shows in designer and in the device when I launch my application. But when I try to change Content property programmatically, for example
UserControl2 control2 = new UserControl2();
myContentControl.Content = control2;
MyContentControl shows nothing. Using standard ContentControl give the same result.
Any suggestions are welcome.
I followed your code to make simple code sample to test. There's no problem.
public class CustomContentControl:ContentControl
{//......}
<Grid>
<local:CustomContentControl x:Name="content">
</local:CustomContentControl>
</Grid>
MyUserControl1 myUserControl1 = new MyUserControl1();
content.Content = myUserControl1;
<UserControl
x:Class="AppContent.MyUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppContent"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<TextBox Text="abc"></TextBox>
</Grid>
You might have done some specific settings in your code. #Martin Zikmund's suggestion also was reasonable. You could refer to his suggestion and check your code. After that, if you still could not solve this issue, please provide a Minimal, Complete, and Verifiable example.
This should work. The reason could be that the control does not stretch and is displayed just 0x0 in size. Try to set absolute Width and Height to the control2 and check if it displays. You can also set myContentControl.HorizontalContentStretch and myContentControl.VerticalContentStretch.
You can try running the app in debugger and then use the Live Property Explorer to see what the actual size of the control inside Content is.
Ok, I found out where the things went wrong. I am using different controls for desktop and mobile devices, so I put some of theirs XAML views to the DeviceFamily-Mobile folder. This way they automatically use when needed. I've confused namespaces, because all XAML views in this folder have a root namespace for accessibility reasons. When I was trying to add control to the ContentControl via c#, I didn't resolve namespace where my controls were placed. So I've put XAML view as a childs to the ContentControl, and they staying invisible as none of them has InitializeComponent() method. Adding correct controls with initialization fixed my problem.
I am very grateful for your answers, they pointed me to the right way.
I am working with wpf page control. Here I need to start application and open page in maximized state.
For window control it is quite easy by setting the state and position, but using page control it is bit tricky.
Below xaml of the first page which is startupuri.
<Page x:Class="MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
Title="MainWindow" ShowsNavigationUI="False" Background="#ffffff">
I am able to height and widht as maximum size using below code:
this.WindowHeight = System.Windows.SystemParameters.MaximizedPrimaryScreenHeight;
this.WindowWidth = System.Windows.SystemParameters.MaximizedPrimaryScreenWidth;
But screens opens at default location with screen overshooting the monitor visual area.
Need some help regarding this issue. thanks
You can set the page's parent Window to maximized state in the page's Loaded event handler, something like:
private void Page_Loaded(object sender, RoutedEventArgs e)
{
(this.Parent as Window).WindowState = WindowState.Maximized;
}
I'm updating a WPF in a project. As I received it, here is how the base window is specified:
<Window x:Class="SomeProject.ButtonForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SomeProject"
Title="ButtonForm" Height="647" Width="379" WindowStyle="None" ResizeMode="CanResizeWithGrip"
SnapsToDevicePixels="True" ShowInTaskbar="False" WindowStartupLocation="CenterScreen"
SizeChanged="Resized" Closing="Window_Closing" AllowsTransparency="True" >
Some main things are that WindowStyle is None, ResizeMode is CanResizeWithGrip and AllowsTransparency is True.
What I would like to do is make this window resizable from all four edges without grips. Is there a way to do this directly in the markup? I've seen some projects that implement this, but they involve whole separate files and complicated code-behind. Certainly there is a simpler way.
Check out MahApps.Metro. It may not be exactly what you're looking for, but it'll give your application an updated look with the functionality you're looking for and takes almost no effort.
You may also want to check out this question for some more discussion about custom window chrome.
You have to set ResizeMode to "CanResize" and AllowTransparency to "False" to be able to see the chrome to resize it.