hide control while resizing window in wpf - c#

I'm trying to hide a control during a window resize because it's not behaving right otherwise. However in WPF there doesn't seem to be any OnBeginResize-ish event.
Any suggestions how to achieve this in WPF?

Bind a property(Notifiable) to your window width so when the width changes Setter of this property will be invoked and within this setter you can have the logic to hide your control.
<Window x:Class="SiemensEnergy.Frw.Main.Client.UI.Views.MainWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewmodel="clr-namespace:SiemensEnergy.Frw.Main.Client.UI.ViewModels"
Title="MainWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Width="{Binding WindowWidthProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
</Window>

Window_SizeChanged?
<Window x:Class="TestControls.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:m="clr-namespace:WFControls;assembly=WFControls"
xmlns:ff="clr-namespace:WFControls.Fernfracht;assembly=WFControls"
Title="MainWindow" Height="350" Width="525" SizeChanged="Window_SizeChanged">
<DockPanel>
</DockPanel>
</Window>

Related

WPF MVVM - How can I pass two variables from the ViewModel associated with the first window to the second ViewModel associated with the second window?

I don't want to use Prism or MVVM Light solutions and would like to pass two parameters to the second window? How could I do that? I was looking for some information on this topic, but unfortunately most of them use packages...
try this:
App.xaml
<Application.Resources>
<vm:MainViewModel x:Key="YourViewModel" />
</Application.Resources>
MainWindow.xaml
<Window x:Class="ABC.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:ABC"
DataContext="{StaticResource YourViewModel}"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBlock Text="{Binding SomeVar}"/>
</Grid>
</Window>
SecondWindow.xaml
<Window x:Class="ABC.SecondWindow"
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:ABC"
DataContext="{StaticResource YourViewModel}"
mc:Ignorable="d"
Title="SecondWindow" Height="450" Width="800">
<Grid>
<TextBox Text="{Binding SomeVar}"/>
</Grid>
</Window>
I guess you could just send parameters in the constructor of the second window, wherever you are creating your window.
var newwin = new SecondWindow("second param is int", 42 );
newwin.Show();
And then just create an another constructor for your window:
public SecondWindow(string a, int b) : this()
{
var secondVM = (SecondViewModel)DataContext;
secondVM.IAmSendingYouTwoParams( a,b);
...
}

Custom Window with all sides Grip

Recently I want to make a custom window with WPF for my program.
Here is the code of the Window:
<Window x:Class="Test.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:Test"
mc:Ignorable="d"
Title="Test" Height="450" Width="850" Background="Transparent" AllowsTransparency="True" WindowStartupLocation="CenterScreen" ResizeMode="CanResizeWithGrip" WindowStyle="None">
</Window>
I want to recreate a titlebar that I set the WindowStyle to None.
Now I need to resize the window with the grip. However, the Grip will not work anymore if I set the WindowStyle to None.
In spite I can set the ResizeMode to CanResizeWithGrip. Whereas, it only works on the bottom right side of the Window.
I want to make the Grip work all sides of the window. How can I achieve it?
You can set the ResizeBorderThickness property of a WindowChrome to make a custom window resizable:
<Window x:Class="WpfApp1.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="450" Width="800" WindowStyle="None">
<WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="0" ResizeBorderThickness="5" />
</WindowChrome.WindowChrome>
<Grid Background="Yellow">
<TextBlock>Resizable window...</TextBlock>
</Grid>
</Window>

how to set background Transparent with WindowChrome in WPF?

I have a wfp which need:
remove the non-client frame from the window
be not included when recording screen
set window background transparent
When I set window with WindowStyle.None, it fails when calling SetWindowDisplayAffinity to exclude it from being recorded. So I customize the window with WindowChrome. It works well, but the window background is black when I set it transparent.
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Background="Transparent"
Title="MainWindow" Height="50" Width="400">
<WindowChrome.WindowChrome>
<WindowChrome x:Name="Chrome"
GlassFrameThickness="0"
ResizeBorderThickness="0"
CaptionHeight="0"/> </WindowChrome.WindowChrome>
</Window>
Anything wrong? Or are there other ways to realize such requirements?

Create window in screen size in wpf

I create a window in wpf and in xml file I write this code:
<Window x:Class="WPFTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Grid >
</Grid>
</Window>
But when I run the project, the size of window is 1080*625. I expect the window size become the screen size, but isn't. Why?
You need to set WindowState to Maximmized:
<Window x:Class="WPFTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" WindowState="Maximized"/>

Autogrow WPF RichTextBox

You can do the trick with TextBox: when you set AcceptReturn=true, the TextBox will grow as you add new lines of text.
Is it possible to do the same to RichTextBox?
I'm doing this in WPF
Yes. By default, RichTextBox accepts return and will grow accordingly. For example, this RichTextBox will grow:
<Window x:Class="stackoverflow___rich_text_box___accepts_return.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">
<StackPanel>
<RichTextBox/>
</StackPanel>
</Window>
You'd have to do this:
<RichTextBox AcceptsReturn="False"/>
to get it not to grow.

Categories