Matthias G.
Matthias G.

Reputation: 71

WPF - How to keep elements in proportion and resize everything

Can someone please tell me how to program actually(!) resizing windows in wpf?

I searched for the solution online, pasted countless code examples, but none of them work like I want them to.

My target is to create a window, that if resized by the user, resizes everything inside, including buttons and text. The window should keep proportions and ideally have a minimum size, but this is purely optional. All solutions I found just resize the window, but not really the elements in it, at least not all. I read somewhere that Stackpanel doesn't really resize, but my tries with Grid also failed and Stackpanel seems a bit easier to arrange contents.

An example of a code I found just resizing the window, but not really the content (at least nor vertically) would be:

<Window x:Class="WPFClient.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:WPFClient"
        mc:Ignorable="d"
        Title="MainWindow" Width="Auto" Height="Auto" SizeToContent="WidthAndHeight">
    <Border Padding="10">
        <StackPanel>

            <!-- Server -->
            <ComboBox SelectedItem="s1" Padding ="2, 2, 2, 2" Margin ="0, 0, 0, 10" MinWidth="220"/>

            <!-- Username -->
            <TextBox x:Name="Input_Username" Height="21" TextWrapping="Wrap" Padding="2">
                <TextBox.Style>
                    <Style TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib">
                        <Style.Resources>
                            <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
                                <VisualBrush.Visual>
                                    <Label Content="Username" Foreground="LightGray" />
                                </VisualBrush.Visual>
                            </VisualBrush>
                        </Style.Resources>
                        <Style.Triggers>
                            <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                                <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                            </Trigger>
                            <Trigger Property="Text" Value="{x:Null}">
                                <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                            </Trigger>
                            <Trigger Property="IsKeyboardFocused" Value="True">
                                <Setter Property="Background" Value="White" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </TextBox.Style>
            </TextBox>

            <!-- Password -->
            <TextBox x:Name="Input_Password" Height="21" TextWrapping="Wrap" Padding="2">
                <TextBox.Style>
                    <Style TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib">
                        <Style.Resources>
                            <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
                                <VisualBrush.Visual>
                                    <Label Content="Password" Foreground="LightGray" />
                                </VisualBrush.Visual>
                            </VisualBrush>
                        </Style.Resources>
                        <Style.Triggers>
                            <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                                <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                            </Trigger>
                            <Trigger Property="Text" Value="{x:Null}">
                                <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                            </Trigger>
                            <Trigger Property="IsKeyboardFocused" Value="True">
                                <Setter Property="Background" Value="White" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </TextBox.Style>
            </TextBox>

            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="2*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <!-- Login-->
                <Button x:Name="WPFLoginButton" Click="WPFLoginButton_Click" Content="Login"  Grid.Column="0" Margin ="0 10 20 10" Padding="2"/>

                <!-- Options-->
                <Button x:Name="WPFOptionsButton" Click="WPFOptionsButton_Click" Content="Options"  Grid.Column="1" Margin ="20 10 0 10" Padding="2"/>
            </Grid>

            <!-- Ses-->
            <TextBlock Text="Ses" FontWeight="Bold"/>
            <TextBox x:Name="SesField" IsReadOnly="True" Background="#eee" Padding="2" Text=""/>
        </StackPanel>

    </Border>
</Window>

A small example with an explanation would be enough, the above code should just demonstrate what I found but don't want.

Upvotes: 1

Views: 355

Answers (2)

Andrius Sakalauskas
Andrius Sakalauskas

Reputation: 36

That's pretty uncommon behavior you want to achieve. However, I think you can do it by wrraping all your controls that you want to resize, inside a Viewbox. Can't give a sample since writing this from phone.

Upvotes: 2

D Smith
D Smith

Reputation: 11

<Window x:Class="WpfComtrolLibTest.Window1"
    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:WpfComtrolLibTest"
    mc:Ignorable="d"
    Title="Window1" Height="450" Width="800">
<Viewbox Margin="0,0,88.2,55.8">
    <Border Padding="10">
        <StackPanel>

            <!-- Server -->
            <ComboBox SelectedItem="s1" Padding ="2, 2, 2, 2" Margin ="0, 0, 0, 10" MinWidth="220"/>

            <!-- Username -->
            <TextBox x:Name="Input_Username" Height="21" TextWrapping="Wrap" Padding="2">
                <TextBox.Style>
                    <Style TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib">
                        <Style.Resources>
                            <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
                                <VisualBrush.Visual>
                                    <Label Content="Username" Foreground="LightGray" />
                                </VisualBrush.Visual>
                            </VisualBrush>
                        </Style.Resources>
                        <Style.Triggers>
                            <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                                <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                            </Trigger>
                            <Trigger Property="Text" Value="{x:Null}">
                                <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                            </Trigger>
                            <Trigger Property="IsKeyboardFocused" Value="True">
                                <Setter Property="Background" Value="White" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </TextBox.Style>
            </TextBox>

            <!-- Password -->
            <TextBox x:Name="Input_Password" Height="21" TextWrapping="Wrap" Padding="2">
                <TextBox.Style>
                    <Style TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib">
                        <Style.Resources>
                            <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
                                <VisualBrush.Visual>
                                    <Label Content="Password" Foreground="LightGray" />
                                </VisualBrush.Visual>
                            </VisualBrush>
                        </Style.Resources>
                        <Style.Triggers>
                            <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                                <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                            </Trigger>
                            <Trigger Property="Text" Value="{x:Null}">
                                <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                            </Trigger>
                            <Trigger Property="IsKeyboardFocused" Value="True">
                                <Setter Property="Background" Value="White" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </TextBox.Style>
            </TextBox>

            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="2*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <!-- Login-->
                <Button x:Name="WPFLoginButton" Click="WPFLoginButton_Click" Content="Login"  Grid.Column="0" Margin ="0 10 20 10" Padding="2"/>

                <!-- Options-->
                <Button x:Name="WPFOptionsButton" Click="WPFOptionsButton_Click" Content="Options"  Grid.Column="1" Margin ="20 10 0 10" Padding="2"/>
            </Grid>

            <!-- Ses-->
            <TextBlock Text="Ses" FontWeight="Bold"/>
            <TextBox x:Name="SesField" IsReadOnly="True" Background="#eee" Padding="2" Text=""/>
        </StackPanel>

    </Border>
</Viewbox>
</Window>

Upvotes: 1

Related Questions