如何禁用一个WPF用户控件的大小调整 [英] How to disable resizing of a UserControl in WPF
本文介绍了如何禁用一个WPF用户控件的大小调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何:
- 禁用调整该用户控件。换句话说,当用户抓住角或该用户控件用鼠标的两侧,我不希望用户能够改变该用户控件的大小?
- 或者,如果有没有办法停止调整大小,然后我怎么只允许用户控件的右侧拖?
<用户控件X:类=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:可忽略=D
D:DesignHeight =152D:DesignWidth =590 HorizontalContentAlignment =右了minWidth ={绑定的ElementName =变量类型}了minHeight ={绑定的RelativeSource = {自我的RelativeSource}}>
<电网WIDTH =591HEIGHT =147了minWidth ={绑定的ElementName = VariableTypeTextBox}>
< Grid.ColumnDefinitions>
< ColumnDefinition WIDTH =137 */>
< ColumnDefinition WIDTH =454 *了minWidth =250/>
< /Grid.ColumnDefinitions>
<按钮内容=取消HEIGHT =23保证金=0,94,7,0NAME =CancelButtonVerticalAlignment =评出的点击=CancelButton_ClickGrid.Column =1的HorizontalAlignment =右WIDTH =75HorizontalContentAlignment =中心VerticalContentAlignment =顶部/>
<按钮内容=创建HEIGHT =23保证金=0,94,108,0NAME =CreateButtonVerticalAlignment =评出的点击=CreateButton_ClickGrid.Column =1的HorizontalAlignment = 右WIDTH =75HorizontalContentAlignment =中心VerticalContentAlignment =顶部/>
<标签内容=变量名HEIGHT =28保证金=0,12,29,0NAME =VARIABLENAMEVerticalAlignment =顶部的HorizontalAlignment =右WIDTH =96目标={结合}HorizontalContentAlignment =右/>
<文本框高度=29保证金=0,11,7,0NAME =VarNameTextBoxVerticalAlignment =顶部的KeyDown =OnKeyDownHandler鼠标离开=的mouseLeaveHandler引发LostFocus =LostFocusHandler网格。柱=1的HorizontalAlignment =弹力/>
<标签内容=变量类型HEIGHT =28保证金=0,0,29,73NAME =变量类型VerticalAlignment =底HorizontalContentAlignment =右的HorizontalAlignment =右宽=96/>
<文本框高度=23保证金=0,51,7,0NAME =VariableTypeTextBoxVerticalAlignment =评出的IsReadOnly =真的背景=银的前景=黑网格。柱=1的HorizontalAlignment =弹力宽度=自动/>
< /网格和GT;
解决方案
您已经粘贴的XAML的用户控件
,但你的问题是问关于窗口
。所以,你需要的地方,它被设置为不允许调整窗口内的用户控件。
一个WPF窗口有一个ResizeMode属性,它可以是一个以下内容:
- NoResize
- CanMinimize
- CanResize(默认)
- CanResizeWithGrip
您会希望NoResize。
例如:
<窗口x:类=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:观点=CLR的命名空间:MyEditor的
MC:可忽略=D
ResizeMode =NoResize
标题=编辑器窗口>
<访问量:MyDialog />
< /窗GT;
请看到的documentation 了解更多详情。
How to:
- 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?
- 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
. 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.
这篇关于如何禁用一个WPF用户控件的大小调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文