Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable resizing of a UserControl in WPF

Tags:

How to:

  1. Disable resizing for this usercontrol. In other words, when the user grabs the corners or the sides of this usercontrol with a mouse, I dont want the user to be able to change the size of the usercontrol?
  2. Or if there is no way to stop resizing then how do I only allow the right side of the usercontrol dragged?
  <UserControl x:Class="MyEditor.MyDialog"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="152" d:DesignWidth="590" HorizontalContentAlignment="Right" MinWidth="{Binding ElementName=VariableType}" MinHeight="{Binding RelativeSource={RelativeSource Self}}">
<Grid Width="591" Height="147" MinWidth="{Binding ElementName=VariableTypeTextBox}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="137*" />
        <ColumnDefinition Width="454*" MinWidth="250" />
    </Grid.ColumnDefinitions>
    <Button Content="Cancel" Height="23" Margin="0,94,7,0" Name="CancelButton" VerticalAlignment="Top" Click="CancelButton_Click" Grid.Column="1" HorizontalAlignment="Right" Width="75" HorizontalContentAlignment="Center" VerticalContentAlignment="Top" />
    <Button Content="Create" Height="23" Margin="0,94,108,0" Name="CreateButton" VerticalAlignment="Top" Click="CreateButton_Click" Grid.Column="1" HorizontalAlignment="Right" Width="75" HorizontalContentAlignment="Center" VerticalContentAlignment="Top" />
    <Label Content="Variable Name " Height="28" Margin="0,12,29,0" Name="VariableName" VerticalAlignment="Top" HorizontalAlignment="Right" Width="96" Target="{Binding}" HorizontalContentAlignment="Right" />
    <TextBox Height="29" Margin="0,11,7,0" Name="VarNameTextBox" VerticalAlignment="Top" KeyDown="OnKeyDownHandler" MouseLeave="MouseLeaveHandler" LostFocus="LostFocusHandler" Grid.Column="1" HorizontalAlignment="Stretch" />
    <Label Content="Variable Type" Height="28" Margin="0,0,29,73" Name="VariableType" VerticalAlignment="Bottom" HorizontalContentAlignment="Right" HorizontalAlignment="Right" Width="96" />
    <TextBox Height="23" Margin="0,51,7,0"  Name="VariableTypeTextBox" VerticalAlignment="Top" IsReadOnly="True" Background="Silver" Foreground="Black" Grid.Column="1" HorizontalAlignment="Stretch"  Width="AUTO" />
</Grid>

like image 496
user1298925 Avatar asked Oct 28 '13 19:10

user1298925


2 Answers

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.

like image 144
devuxer Avatar answered Sep 20 '22 12:09

devuxer


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

like image 41
WAQ Avatar answered Sep 17 '22 12:09

WAQ