Rubber window WPF - c#

I create window with set Height and Width. Current content are placed in window. But when I change content rather big then previously it dont placed in window. How create rubber window who automatically change size under the desired?
<Window x:Class="GidroTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:gidro_test="clr-namespace:GidroTest"
Title="MainWindow" Height="200" Width="200"
WindowStartupLocation="CenterScreen"
>
<DockPanel>
<!-- Before click -->
<Rectangle Width="199" Height="199" Click="Change_size_Click"/>
<!-- After click -->
<Rectangle Width="300" Height="300" Click="Change_size_Click"/>
</DockPanel>
</Window>

You can try set SizeToContent with: WidthAndHeight, Width, Height, don't forget remove Height, Width from Window
<Window SizeToContent="WidthAndHeight">
Details: https://msdn.microsoft.com/en-us/library/system.windows.window.sizetocontent%28v=vs.110%29.aspx

Related

How to add multiple content control on same window [duplicate]

Problem:
XAML file:
<Window
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"
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" x:Class="WpfApp1.MainWindow"
mc:Ignorable="d"
Title="Test" Height="398.775" Width="782" BorderBrush="Black" Margin="0" IsTabStop="True">
<Window.TaskbarItemInfo>
<TaskbarItemInfo/>
</Window.TaskbarItemInfo>
<TextBox HorizontalAlignment="Left" Height="23" Margin="268,290,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Window>
A Window is a ContentControl. These have a Content property to display a single item. If you want to display multiple items, use one of the following built-in layout panels and drag them onto your window. Then you can add multiple controls to them.
Grid
StackPanel
WrapPanel
DockPanel
UniformGrid
Canvas
Because you're on the window who only accepts 1 element, you need first add a content controller .
It can be:
Grid
Canvas
Dock Panel
StackPanel
WrapPannel
Scroll viewer
Add <grid> </grid> in Window tag then you can add your controls in it.

Why can't I put more than one control on a window?

Problem:
XAML file:
<Window
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"
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" x:Class="WpfApp1.MainWindow"
mc:Ignorable="d"
Title="Test" Height="398.775" Width="782" BorderBrush="Black" Margin="0" IsTabStop="True">
<Window.TaskbarItemInfo>
<TaskbarItemInfo/>
</Window.TaskbarItemInfo>
<TextBox HorizontalAlignment="Left" Height="23" Margin="268,290,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Window>
A Window is a ContentControl. These have a Content property to display a single item. If you want to display multiple items, use one of the following built-in layout panels and drag them onto your window. Then you can add multiple controls to them.
Grid
StackPanel
WrapPanel
DockPanel
UniformGrid
Canvas
Because you're on the window who only accepts 1 element, you need first add a content controller .
It can be:
Grid
Canvas
Dock Panel
StackPanel
WrapPannel
Scroll viewer
Add <grid> </grid> in Window tag then you can add your controls in it.

Desire specific size of Visual Studio 2019 Extension ToolWindow ( WPF UserControl )

I am looking to get a fix size windows because of content that I am showing. I am struggling because when the extension is run the size of windows is crop to a fix size. The simple example is just modified the default and try to make it 800x600 window. Once the window is up on screen, you can manual adjust and next time it will be fine but I wish to make sure the default is correct.
I seen other questions about like not having fix size and such and nothing seems to work. I understand if user changes the size smaller than it will be that may - but I would like default size to be bigger
<UserControl x:Class="VSIXProject1.ToolWindow1Control"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vsshell="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.15.0"
Background="{DynamicResource {x:Static vsshell:VsBrushes.WindowKey}}"
Foreground="{DynamicResource {x:Static vsshell:VsBrushes.WindowTextKey}}"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="800"
Name="MyToolWindow">
<Grid MinWidth="800" MinHeight="600">
<StackPanel Orientation="Vertical" Width="800" Height="600">
<TextBlock Margin="10" HorizontalAlignment="Center">ToolWindow1</TextBlock>
<Button Content="Click me!" Click="button1_Click" Width="120" Height="80" Name="button1"/>
</StackPanel>
</Grid>
Source Image
Problem Image
I try following code in Package class as recommended
[ProvideToolWindow(typeof(ToolWindow1),
Width =800,
Height =600,
Style = Microsoft.VisualStudio.Shell.VsDockStyle.AlwaysFloat)]
You can stipulate the initial size of a toolwindow by setting the Width and Height properties, via the ProvideToolWindowAttribute, used to register your toolwindow. Toolwindows are "sticky", with the position, size, dockstate etc being persisted with your other window layout options. Meaning, once the user resizes/docs/floats the toolwindow, it's state gets saved. But you can readily reset it back to the default, using the Windows | Reset Window Layout menu command.
Sincerely,
Hi you can set the Window size in this way
<UserControl x:Class="VSIXProject1.ToolWindow1Control"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vsshell="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.15.0"
Background="{DynamicResource {x:Static vsshell:VsBrushes.WindowKey}}"
Foreground="{DynamicResource {x:Static vsshell:VsBrushes.WindowTextKey}}"
MinWidth = "800"
MinHeight = "600"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="800"
Name="MyToolWindow">
Set the MinWidth and MinHeight will ensure that window will never go under that dimension

Set Dockpanel width and height as same as Window width and height in wpf

I have created main window in this window created dockpanel for binding user control value in main window like as below,
<Window x:Class="WpfApplication2.DMMainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ResizeMode="NoResize"
WindowState="Maximized"
WindowStyle="None"
WindowStartupLocation="CenterScreen"
Height="{Binding SystemParameters.PrimaryScreenHeight}"
Width="{Binding SystemParameters.PrimaryScreenWidth}">
<DockPanel Width="1254" Height="1200" HorizontalAlignment="Left" Margin="0,0,0,0" x:Name="mainPanel" VerticalAlignment="Top" />
</Window>
In this code you can see given width and height for dockpanel. I need this height and width need to bind as same as window width. I have used actual width and height and also sizetocontent but nothing happened as expected. Please give your suggestion.
As you are using dock panel you do not need to set it explicitly. Incase you want , you can restrict further by using minimum height and width, but no mandatory.
<Window x:Class="WpfApplication2.DMMainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ResizeMode="NoResize"
WindowState="Maximized"
WindowStyle="None"
WindowStartupLocation="CenterScreen"
Height="{Binding SystemParameters.PrimaryScreenHeight}"
Width="{Binding SystemParameters.PrimaryScreenWidth}">
<DockPanel x:Name="mainPanel"
MinHeight ="{Binding SystemParameters.PrimaryScreenHeight}"
MinWidth ="{Binding SystemParameters.PrimaryScreenWidth}" />
</Window>
You can add Stretch property as below,
<DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,0,0" x:Name="mainPanel" />
Hope it will work
<Window x:Class="WpfApplication2.DMMainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ResizeMode="NoResize"
WindowState="Maximized"
WindowStyle="None"
WindowStartupLocation="CenterScreen"
Height="{Binding SystemParameters.PrimaryScreenHeight}"
Width="{Binding SystemParameters.PrimaryScreenWidth}">
<DockPanel x:Name="mainPanel" />
</Window>

WPF Cannot scroll an image inside a ScrollViewer

I try to display an image in my window :
<Window x:Class="Problem.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">
<DockPanel>
<StackPanel Orientation="Vertical">
<ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
<Image Source="cat.jpg" Stretch="Uniform">
<Image.LayoutTransform>
<RotateTransform Angle="90" />
</Image.LayoutTransform>
</Image>
</ScrollViewer>
</StackPanel>
</DockPanel>
</Window>
where cat.jpg is a 1920x1080 image.
Here is the result :
As you can see, the VerticalScrollbar is disabled although I can't see the full cat head. Moreover, HorisontalScrollBar is Invisible.
My question is : How to enable scrollbar in order to scroll over my image ?
Remove the StackPanel. It gives its content infinite space, so the ScrollViewer has the height of the image. If you need to stack something under the image, create a StackPanel inside the ScrollViewer:
<Window x:Class="Problem.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">
<DockPanel>
<ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
<StackPanel>
<Image Source="cat.jpg" Stretch="Uniform">
<Image.LayoutTransform>
<RotateTransform Angle="90" />
</Image.LayoutTransform>
</Image>
</StackPanel>
</ScrollViewer>
</DockPanel>
I was having the same problem, but with a custom made Image class which I only rendered drawings on by using the DrawingContext in the protected override void OnRender(DrawingContext dc) function. I did not understood then, that I either needed to set the size of the image (set the Width and Height properties) or create a new image from the drawingContext and make it the Source of the image, in order to resize the actual image.
I got my answer from here: Get images from DrawingGroup and the way I solved it was by changing the properties every time I used the Render function of my image:
DrawingImage drawingImage = new DrawingImage(mBackingStore);
Width = drawingImage.Width;
Height = drawingImage.Height;

Categories