2013-10-28 26 views
12

禁用用戶控件的大小調整如何:如何在WPF

  1. 禁用調整該用戶控件。換句話說,當用戶用鼠標抓住這個用戶控件的邊或邊時,我不希望用戶能夠改變usercontrol的大小?
  2. 或者如果沒有辦法停止調整大小,那麼我怎樣才能讓usercontrol的右側被拖拽?
<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> 

回答

20

您貼上了XAML的UserControl,但你的問題是問一個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瞭解更多詳情。

+0

謝謝。我確實也把我的問題用戶控制。 – user1298925

+0

它是否可以將您的UserControl放置在一個窗口中,正如我在答案中所顯示的那樣? – devuxer

+0

因爲在我創建UserControl的代碼中崩潰了,但現在我正在創建一個Window。如果有一種方法可以使用戶控件不可調整大小,那將是所需的路徑。有任何想法嗎?提前致謝。 – user1298925

2

只需將MinWidth/MaxWidth和MinHeight/MaxHeight屬性設置爲所需的值即可。

+0

謝謝。我爲UserControl設置了最大值和最小值,但這不起作用。 – user1298925

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