C
C#16mo ago
AceChewy

❔ Help with minimising, maximisng and closing a window

I add a close, maximise and minimise button to my application, however they don't seem to be working. Can I get some help?
<Window x:Class="Utiliites_App.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:Utiliites_App"
xmlns:viewModel="clr-namespace:Utiliites_App.MVVM.ViewModel"
mc:Ignorable="d"
Height="600" Width="970"
WindowStyle ="None"
ResizeMode="CanResize"
Background= "Transparent"
AllowsTransparency="True">
<Window x:Class="Utiliites_App.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:Utiliites_App"
xmlns:viewModel="clr-namespace:Utiliites_App.MVVM.ViewModel"
mc:Ignorable="d"
Height="600" Width="970"
WindowStyle ="None"
ResizeMode="CanResize"
Background= "Transparent"
AllowsTransparency="True">
<Grid HorizontalAlignment="Right" Grid.Column="2">
<StackPanel Orientation="Horizontal">
<Button Width="20"
Height="20"
Content="-"
Background="Transparent"
BorderThickness="0"
Foreground="White"
Command="{Binding MinimizeWindowCommand}" Margin="5"/>


<Button Width="20"
Height="20"
Content="□"
Background="Transparent"
BorderThickness="0"
Foreground="White"
Command="{Binding MaximizeWindowCommand}" />

<Button Width="20"
Height="20"
Content="x"
Background="Transparent"
BorderThickness="0"
Foreground="White"
Command="{Binding CloseWindowCommand}" Margin="5"/>
</StackPanel>
</Grid>
<Grid HorizontalAlignment="Right" Grid.Column="2">
<StackPanel Orientation="Horizontal">
<Button Width="20"
Height="20"
Content="-"
Background="Transparent"
BorderThickness="0"
Foreground="White"
Command="{Binding MinimizeWindowCommand}" Margin="5"/>


<Button Width="20"
Height="20"
Content="□"
Background="Transparent"
BorderThickness="0"
Foreground="White"
Command="{Binding MaximizeWindowCommand}" />

<Button Width="20"
Height="20"
Content="x"
Background="Transparent"
BorderThickness="0"
Foreground="White"
Command="{Binding CloseWindowCommand}" Margin="5"/>
</StackPanel>
</Grid>
28 Replies
AceChewy
AceChewyOP16mo ago
public ICommand MinimizeWindowCommand { get; }
public ICommand MaximizeWindowCommand { get; }
public ICommand CloseWindowCommand { get; }


private void MinimizeWindow()
{
Application.Current.MainWindow.WindowState = WindowState.Minimized;
}

private void MaximizeWindow()
{
if (Application.Current.MainWindow.WindowState == WindowState.Maximized)
Application.Current.MainWindow.WindowState = WindowState.Normal;
else
Application.Current.MainWindow.WindowState = WindowState.Maximized;
}

private void CloseWindow()
{
Application.Current.MainWindow.Close();
}
public ICommand MinimizeWindowCommand { get; }
public ICommand MaximizeWindowCommand { get; }
public ICommand CloseWindowCommand { get; }


private void MinimizeWindow()
{
Application.Current.MainWindow.WindowState = WindowState.Minimized;
}

private void MaximizeWindow()
{
if (Application.Current.MainWindow.WindowState == WindowState.Maximized)
Application.Current.MainWindow.WindowState = WindowState.Normal;
else
Application.Current.MainWindow.WindowState = WindowState.Maximized;
}

private void CloseWindow()
{
Application.Current.MainWindow.Close();
}
sibber
sibber16mo ago
this shouldnt be in the viewmodel imo also what does "not work" mean what happens when you click them
AceChewy
AceChewyOP16mo ago
Nothing, as is in you press them and there's no change
sibber
sibber16mo ago
all of them?
AceChewy
AceChewyOP16mo ago
Yup
sibber
sibber16mo ago
is the command being executed? put a breakpoint
AceChewy
AceChewyOP16mo ago
I don't think so Should I add the breakpoint, after the command is executed
sibber
sibber16mo ago
put it on the opening bracket
AceChewy
AceChewyOP16mo ago
I have, I'll be gone for 15 sorry, need to sort something out
sibber
sibber16mo ago
np
nohopestage
nohopestage16mo ago
Yeah, just put it in code-behind @AceChewy And remove the Application.Current.MainWindow part
sibber
sibber16mo ago
yeah make it reference the current window and close the window not the application but thats after we figure out the current issue
SuperBrain
SuperBrain16mo ago
Just as a side-note, to save yourself some time, you may want to use one of the ready-made $wpfuilibs
SuperBrain
SuperBrain16mo ago
Most of them offer title bar area customization, including extending the window client area to title bar area. Speaking of your commands not being executed, you either did not set the appropriate DataContext or did not properly bind the commands to your buttons.
sibber
sibber16mo ago
this looks like a custom design anyway
AceChewy
AceChewyOP16mo ago
Ok, I’ll try they in a minute Can’t get back to my computer for a while so I’ll try it later and let you know if it works
Insire
Insire16mo ago
you define those commands, but nothing actually initializes them with methods you defined later you also dont seem to set the datacontext of your window anywhere meaning, even if you initialized the commands somewhere, the bindings wont work, because they have nothing to bind to as you didnt provide a source for your binding
AceChewy
AceChewyOP16mo ago
How would I go about setting the data context of my window
Insire
Insire16mo ago
its literally a property, so you assign it a value like anywhere else in c# code yourWindow.DataContext = someThing;
AceChewy
AceChewyOP16mo ago
Where would be an appropriate place to set the context?
nohopestage
nohopestage16mo ago
In App.xaml.cs if you use DI, otherwise in .xaml or .xaml.cs of your window
AceChewy
AceChewyOP16mo ago
Right I've done what you've said I already had the data context set in the xaml file, completely forgot about that And it works now, so great Before I close this thread, do any of you know how I can change the name of my applicaiton without having to manually change it everywhere in the code where it's referenced?
<Window x:Class="Utiliites_App.MainWindow"
<Window x:Class="Utiliites_App.MainWindow"
I realised I mispelt Utilities
AceChewy
AceChewyOP16mo ago
Help with how to get the application name to display in the tab below would also be helpful:
Korbah
Korbah16mo ago
You would just set a property on the viewmodel that has your application name and bind to that
nohopestage
nohopestage16mo ago
Just set window title
AceChewy
AceChewyOP16mo ago
Thanks
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server