How to disable resizing of a UserControl in WPF

You've pasted the XAML for a UserControl, but your question is asking about a Window. So, you will need to place your UserControl inside a Window that is set up to not allow resizing.

A WPF Window has a ResizeMode property, which can be one of the following:

  • NoResize
  • CanMinimize
  • CanResize (default)
  • CanResizeWithGrip

You will want NoResize.

Example:

<Window x:Class="MyEditor.Views.EditorWindow"
        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:views="clr-namespace:MyEditor"
        mc:Ignorable="d"
        ResizeMode="NoResize"
        Title="Editor Window">
    <views:MyDialog />
</Window>

Please see the documentation for more details.


For Disabling : ResizeMode="CanMinimize"

<Window x:Class="XXXXXX.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:XXXXXXX"
        mc:Ignorable="d"
        WindowState="Maximized"
        ResizeMode="CanMinimize"
        Title="Window">

Simply set the MinWidth/MaxWidth and MinHeight/MaxHeight properties to your required value.


Here is a simple solution:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    ((Window)Parent).ResizeMode = ResizeMode.NoResize;
}

Or an attached property on the UserControl if you use the Prism Library:

<prism:Dialog.WindowStyle>
    <Style TargetType="Window">
        <Setter Property="prism:Dialog.WindowStartupLocation" 
            Value="CenterScreen" />
        <Setter Property="ResizeMode" Value="NoResize"/>
        <Setter Property="ShowInTaskbar" Value="False"/>
        Setter Property="SizeToContent" Value="WidthAndHeight"/>
    </Style>
</prism:Dialog.WindowStyle>

Tags:

C#

Wpf

Xaml