I have 2 windows now that i want to open maximized but should be non realizable?how would i do this?
To maximize the window use:
this.WindowState = System.Windows.WindowState.Maximized;
This will allow the window only to be minimized:
this.ResizeMode = System.Windows.ResizeMode.CanMinimize;
You can change it to the following if you want to disable minimizing as well:
this.ResizeMode = System.Windows.ResizeMode.NoResize;
Place the above in your form's constructor, it should look something like this:
public MainWindow()
{
InitializeComponent();
this.WindowState = System.Windows.WindowState.Maximized;
this.ResizeMode = System.Windows.ResizeMode.CanMinimize;
}
Or simply add the following properties to your window in XAML to make it look like this:
Title="MainWindow" ResizeMode="NoResize" WindowState="Maximized">
This can be done in WPF by following way:
window.WindowState = WindowState.Maximized;
In XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
WindowState="Maximized">
...
</Window>
Related
Сan't maximize window when using MahApps.Metro window, the size does not change. Example of my window:
<controls:MetroWindow x:Class="Example.View.HelpView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:uc="clr-namespace:Example.UserControls"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d"
Height="843"
Width="1258"
WindowStyle="None" Background="Transparent">
</controls:MetroWindow>
Edit
I created button for this and trying to change the state as follows:
private void UserControl_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Window _window = Window.GetWindow(this);
if (_window.WindowState == WindowState.Maximized)
{
_window.WindowState = WindowState.Normal;
}
else
{
_window.WindowState = WindowState.Maximized;
}
}
Edit
When I expand the window to full screen, it behaves as follows, takes up the same space as in normal mode, but the rest of the window is locked
enter image description here
lastly do you try this :) I think "_window" not seting correctly now
xaml
<controls:MetroWindow
x.name="MetroWindow"
</controls:MetroWindow>
c#
Window _window = MetroWindow;
or
c#
Window _window = (Window)sender;
I'm trying to display a window that aligns with an existing component. In this example I want to align it to a button. When i click the button I would like the window to position itself so that it's bottom is just above the button, and it's width is the same as the button. The left of the window should be the same as the left of the button.
To achieve this I use the following xaml:
<Window x:Class="WindowPositioningTest.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"
xmlns:local="clr-namespace:WindowPositioningTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Name="MyButton" Content="Click me to see window!" Width="300" Height="50" Click="Button_Click"/>
</Grid>
The onclick function looks like this:
private void Button_Click(object sender, RoutedEventArgs e)
{
var window = new Window();
var myButtonLocation = MyButton.PointToScreen(new Point(0, 0));
window.Width = MyButton.ActualWidth;
window.Height = 300;
window.Left = myButtonLocation.X;
window.Top = myButtonLocation.Y - window.Height;
window.Show();
}
When I click the button a window is displayed like in the picture below.
My question is: why is the window not as wide as the button and why is it not in the right position? It's almost as if there's an invisible frame around the window.
Try this:
private void Button_Click(object sender, RoutedEventArgs e)
{
var window = new Window();
var myButtonLocation = MyButton.PointToScreen(new Point(0, 0));
window.Width = MyButton.ActualWidth + 16;
window.Height = 300;
window.Left = myButtonLocation.X - 8;
window.Top = myButtonLocation.Y - window.Height;
window.Show();
}
It happens beacuse of window border. As you know, window is a composite element. I think when you set Width, you set a width of the working space, not the width of the whole window.
How can I be able to see the taskbar when WindowStyle="None". I'm trying to have my own buttons (Close, Maximize, Minimize) by removing the actual window title bar and using a dll too remove the border. Easy to maintain and easy to put in my code would be greatly appreciated.
One of the possible fix to do this is: to limit the max size of window. For example:
C# code:
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
if (WindowState == WindowState.Normal)
{
System.Drawing.Rectangle rec = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle).WorkingArea;
MaxHeight = rec.Height;
MaxWidth = rec.Width;
ResizeMode = ResizeMode.NoResize;
WindowState = WindowState.Maximized;
}
else
{
MaxHeight = double.PositiveInfinity;
MaxWidth = double.PositiveInfinity;
ResizeMode = ResizeMode.CanResize;
WindowState = WindowState.Normal;
}
}
}
You need to set maxsize just before changing of window state. Otherwise, in some cases it works wrong. Also don't forget about ResizeMode.
And xaml:
<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" WindowStyle="None" Height="300" Width="300">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="75" Click="ButtonBase_OnClick"/>
</Grid>
</Window>
I want create a new Window beside an existing main Windwoe with a scrollable Textbox.
I'm pressing in my main Window on a button "Open New Window" and then it should open a new Window with a scrollable Textbox.
inside form2
In WPF you can drag drop elements in the main Window but cant do that for a new window.
So I thought it is only possible when you create a new window in the MainWindow.xaml.cs
I was able to create a new Window trough:
private void btnConnect_Click(object sender, RoutedEventArgs
{
Form form2 = new Form();
//Do intergreate TextBox with scrollbar in form2
form2.Show();
}
and now I want a Textbox
But how can I do that in C# or WPF?
Thx
well... you can create a new Window and load into this Windows.Content a UserControl wich you createt in a new XAML.
Example:
NewXamlUserControl ui = new NewXamlUserControl();
MainWindow newWindow = new MainWindow();
newWindow.Content = ui;
newWindow.Show();
the Xaml is could be like this
<UserControl x:Class="Projekt"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="newXamlUserControl"
Height="300" Width="300">
<Grid>
<TextBox Text = ..../>
</Grid>
</UserControl>
Create a new WPF window in your project:
Project -> Add New Item -> Window (WPF)
Name the window appropriately (here I use ConnectWindow.xaml)
Add a TextBox to the XAML
<Window
x:Class="WpfApplication1.ConnectWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Connect"
Height="300"
Width="300"
ShowInTaskbar="False">
<TextBox
AcceptsReturn="True"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"/>
</Window>
You can customize both Window and TextBox as you like.
There are several ways to display the window.
Displaying a modal window (this refers to the main window):
var window = new ConnectWindow { Owner = this };
window.ShowDialog();
// Execution only continues here after the window is closed.
Displaying a modeless child window:
var window = new ConnectWindow { Owner = this };
window.Show();
Displaying another top-level window:
var window = new ConnectWindow();
window.Show();
I would like to make my WPF application fullscreen. Right now the start menu prevents it from covering everything and shifts my application up. This is what I have for my MainWindow.xaml code:
<Window x:Class="HTA.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
mc:Ignorable="d"
WindowStyle="None" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen"
Width="1024" Height="768">
You're probably missing the WindowState="Maximized", try the following:
<Window x:Class="HTA.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
WindowStyle="None" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Window x:Class="HTA.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
mc:Ignorable="d"
ResizeMode="NoResize"
WindowStartupLocation="CenterScreen"
Width="1024" Height="768"
WindowState="Maximized" WindowStyle="None">
Window state to Maximized and window style to None
You can also do it at run time as follows :
Assign name to the window (x:Name = "HomePage")
In constructor just set WindowState property to Maximized as follows
HomePage.WindowState = WindowState.Maximized;
window.WindowStyle = WindowStyle.None;
window.ResizeMode = ResizeMode.NoResize;
window.Left = 0;
window.Top = 0;
window.Width = SystemParameters.VirtualScreenWidth;
window.Height = SystemParameters.VirtualScreenHeight;
window.Topmost = true;
Works with multiple screens
When you're doing it by code the trick is to call
WindowStyle = WindowStyle.None;
first and then
WindowState = WindowState.Maximized;
to get it to display over the Taskbar.
If you want user to change between WindowStyle.SingleBorderWindow and WindowStyle.None at runtime you can bring this at code behind:
Make application fullscreen:
RootWindow.Visibility = Visibility.Collapsed;
RootWindow.WindowStyle = WindowStyle.None;
RootWindow.ResizeMode = ResizeMode.NoResize;
RootWindow.WindowState = WindowState.Maximized;
RootWindow.Topmost = true;
RootWindow.Visibility = Visibility.Visible;
Return to single border style:
RootWindow.WindowStyle = WindowStyle.SingleBorderWindow;
RootWindow.ResizeMode = ResizeMode.CanResize;
RootWindow.Topmost = false;
Note that without RootWindow.Visibility property your window will not cover start menu, however you can skip this step if you making application fullscreen once at startup.