简体   繁体   English

如何在 WPF 中禁用调整用户控件的大小

[英]How to disable resizing of a UserControl in WPF

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>

You've pasted the XAML for a UserControl , but your question is asking about a Window .您已为UserControl粘贴了 XAML,但您的问题是询问Window So, you will need to place your UserControl inside a Window that is set up to not allow resizing.因此,您需要将 UserControl 放置在设置为不允许调整大小的窗口中。

A WPF Window has a ResizeMode property, which can be one of the following: WPF 窗口有一个 ResizeMode 属性,它可以是以下之一:

  • NoResize不调整大小
  • CanMinimize可以最小化
  • CanResize (default) CanResize(默认)
  • CanResizeWithGrip CanResizeWithGrip

You will want NoResize.你会想要 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.有关更多详细信息,请参阅文档

只需将 MinWidth/MaxWidth 和 MinHeight/MaxHeight 属性设置为您需要的值。

For Disabling : ResizeMode="CanMinimize"禁用: 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">

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 库,或者 UserControl 上的附加属性:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM