initial size of element wpf - wpf

I have window with this markup
<Window x:Class="TestCloseWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Button Command="ApplicationCommands.Close">Foo</Button>
</Window>
I'd like to button have MinWidth and MinHeight 100
and MaxHeight and MinHeight equals to 500
And like size window to content so I make this
<Window x:Class="TestCloseWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" SizeToContent="WidthAndHeight" >
<Button MaxHeight="500" MaxWidth="500" MinHeight="100" MinWidth="100" Command="ApplicationCommands.Close">Foo</Button>
</Window>
And I'd like to set initial size of button 200 x 200
without writing Width and Height in window
If I write Width="200" Height="200" in button, button becomes unresizable.
How can I set initial size of control?

I am not sure exactly what you are trying to accomplish, but see if this does what you are wanting. I am using a ViewBox to stretch the Contents when the Window is resized, and setting the contraints in the Window. Be aware that Window Size is different than Window Client Area size.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" MaxHeight="500" MaxWidth="500"
MinWidth="100" MinHeight="100"
Width="200" Height="200">
<Viewbox StretchDirection="Both" Stretch="Fill">
<Button >Hello</Button>
</Viewbox>

Related

WPF window corner radius and shadow

I want to have a window without buttons, with rounded corners and with shadow.
Like this:
So this is my XAML code:
<Window x:Class="Password.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:Password"
mc:Ignorable="d"
Title="MainWindow" Height="294" Width="546" Background="Transparent" Foreground="#FFE755DD" AllowsTransparency="True" WindowStyle="None" WindowStartupLocation="CenterScreen">
<Grid >
<Grid.Effect>
<DropShadowEffect BlurRadius="20" Direction="-90" RenderingBias="Quality" ShadowDepth="2"/>
</Grid.Effect>
<Border Background="#FF22173C" CornerRadius="20, 20, 20, 20"/>
</Grid>
</Window>
And this is the output:
The shadow isn't rounded.
What should I do?
The blur gets cropped because your window doesn't own the space around it.
Set <Grid Margin="20"> to allow the blur to occur in the area you control.

Set Button Size to Content Size in WPF Application

I have the following code in WPF XAML:
<Window x:Class="WPFDemo.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>
<Button Content="Hello World" Width="Auto" Height="Auto"></Button>
</StackPanel>
</Window>
I have set the width and height of the Button to "Auto" but still it stretches the complete horizontal width. What am I doing wrong? I do not want to provide a hardcoded values for the width and height!
You are using the wrong kind of container control. The StackPanel does not resize its contents. Try a Grid instead:
<Grid>
<Button Content="Hello" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
Also, you don't need to set the Width and Height to "Auto", as the default values will enable the Button to stretch to fill its parent container (depending on the container control). Please see the Panels Overview page on MSDN for more help with the differences between the various container controls in WPF.
As Sheridan said, you do not need to set Width and Height to "Auto".
Your problem is that the default alignment of the StackPanel content is "Stretch".
So just set the HorizontalAlignment property of your button to "Left" or "Right".
<Window x:Class="WPFDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Grid>
<Button Content="Hello World" Width="Auto" Height="Auto"></Button>
</Grid>
</Window>

WPF: Textblock text in Popup goes outside of the main window

In my example wpf app I've added one button and one popup to the window. The button is in the bottom right corner and the popup has set "PlacementTarget" property to it and "Placement" set to top. The popup consists of one very long textblock.
What I expect this popup will behave is not to go outside of the window and therefore automatically set his "HorizontalOffset" to the appropriate value, but the popup behaves against my intentions.
Here's my xaml file:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1" x:Name="window" x:Class="WpfApplication1.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:Converters x:Key="Converters"/>
</Window.Resources>
<Grid>
<Button x:Name="button" Content="Button" VerticalAlignment="Bottom" Width="75" HorizontalAlignment="Right"/>
<Popup Placement="Top" PlacementTarget="{Binding ElementName=button, Mode=OneWay}" IsOpen="True">
<TextBlock TextWrapping="Wrap" Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" Background="White"/>
</Popup>
</Grid>
Do anyone know how to fix it?
I've read that this should be default popup behavior to take care of going out of the boundaries, but not in my case. Thanks in advance.
Have you tried to set the width of the Popup or Textblock ?
Sorry, I can't write this poor answer as a comment..

Resize canvas to window / user control size

Hi I convert SVG image to XAML/canvas. I would like set this canvas as window/user control background.
Something like this:
<Window x:Class="WpfApplication2.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">
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Name="svg2383" Width="800" Height="600">
<---->
</Canvas>
</Window>
My problem is Canvas is too much big, I would like automatic resize/stretch canvas on window height/width.
Image on canvas is complicated..http://kde-look.org/content/show.php/something_wall?content=115863
So set Width and Height is not solution, because this canvas consist other canvas.
How about removing specified width and height
<canvas Name="svg2383">
<---->
</canvas>
EDIT: Another way is to use a binding as follows:
<Window x:Name="MainWin">
<Canvas Width="{Binding ElementName=MainWin, Path=ActualWidth}"
Height="{Binding ElementName=MainWin, Path=ActualHeight}">
<--->
</Canvas>
</Window>
You can put the canvas within a Viewbox. This will allow you to stretch it to fill the window, while designing it at a constant size.

Is there any way we can Automatically Stretch Horizontally or Vertically Childrens in Canvas like Grid does?

Is there any way whether we can stretch Element placed inside a Convas if we give the Horizotal and Vertical Alignment properties to Stretch?
You could try something like this:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Canvas Name="theCanvas">
<Button Content="Hello" Width="{Binding ActualWidth, ElementName=theCanvas}" />
</Canvas>
</Window>

Resources