如何在WPF中禁用UserControl的大小调整

时间:2013-10-28 19:06:23

标签: c# wpf xaml

如何:

  1. 禁用此用户控件的大小调整。换句话说,当用户用鼠标抓住此用户控件的角落或侧面时,我不希望用户能够更改用户控件的大小?
  2. 或者如果无法停止调整大小,那么我如何才允许拖动用户控件的右侧?
  3.   <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>
    

4 个答案:

答案 0 :(得分:26)

您已为UserControl粘贴了XAML,但您的问题是询问Window。因此,您需要将UserControl放置在一个设置为不允许调整大小的窗口中。

WPF窗口有一个ResizeMode属性,可以是以下之一:

  • NoResize
  • CanMinimize
  • CanResize(默认)
  • CanResizeWithGrip

你会想要NoResize。

示例:

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

有关详细信息,请参阅documentation

答案 1 :(得分:3)

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

答案 2 :(得分:0)

禁用: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">

答案 3 :(得分:0)

这是一个简单的解决方案:

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

或者,如果您使用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>