tete
tete

Reputation: 4999

How to only allow WPF Window's height resizable?

To be specific, I have a Window whose Content is set as a UserControl. And I would like to allow users to drag to increase the height of the Window, but the Width should be fixed and non-resizable. I guess I have such properties to modify but I can't make them behave as I desired:

But what kind of combination can achieve what I want?

Upvotes: 1

Views: 2519

Answers (2)

Istiaq Ahmed
Istiaq Ahmed

Reputation: 156

Using the WindowChrome.ResizeBorderThickness property, this can be achieved in a much simpler fashion without having to use MinWidth, MaxWidth and ActualHeight properties. For example, consider the following style that you can use with your window to allow resizing height only:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:System="clr-namespace:System;assembly=mscorlib"
    x:Class="YourResourceDictionaryClass" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:shell="http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell">

<Style x:Key="AllowResizeHeightOnlyWindow" TargetType="{x:Type Window}">
<Setter Property="shell:WindowChrome.WindowChrome">
            <Setter.Value>
                <shell:WindowChrome ResizeBorderThickness="0, 2"
                            />
            </Setter.Value>
        </Setter>
</Style>

Hope this helps

Upvotes: 0

user128300
user128300

Reputation:

One way to do that would be to set both MinWidth and MaxWidth of the window to the same value:

<Window x:Class="WpfApplication20.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" MinWidth="525" MaxWidth="525">
    <Grid>

    </Grid>
</Window>

EDIT: If you want to do it in code-behind, as you wrote in your comment, you could do it as follows:

<Window x:Class="WpfApplication20.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350">
    <Rectangle x:Name="childElement"  Width="525" Fill="Red">

    </Rectangle>
</Window>

And in the window's constructor, you would write:

public MainWindow()
{
    InitializeComponent();

    Binding binding = new Binding("ActualWidth") { Source = childElement };
    BindingOperations.SetBinding(this, MinWidthProperty, binding);
    BindingOperations.SetBinding(this, MaxWidthProperty, binding);

}

Please note: the childElement above would be your UserControl.

Upvotes: 5

Related Questions