如何在 WPF 中禁用调整用户控件的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21306082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable resizing of user control in WPF
提问by UserK
I have Usercontrol.I want to disable its resizing. The usercontrol is:
我有用户控件。我想禁用它的大小调整。用户控件是:
<UserControl x:Class="DocumentUpload"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
xmlns:telerikGrid1="clr-namespace:Telerik.Windows.Controls.GridView;assembly=Telerik.Windows.Controls.GridView"
xmlns:telerikInp="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input"
xmlns:telerikNav="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"
xmlns:telerikData="clr-namespace:Telerik.Windows.Data;assembly=Telerik.Windows.Data"
xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Height="auto" Width="auto" MaxWidth="520">
I got to know that there is property called
我知道有一个属性叫做
ResizeMode="NoResize"
ResizeMode="NoResize"
.But it is not available in UserControl.Any suugestion?
.但它在 UserControl 中不可用。有任何建议吗?
采纳答案by XAMeLi
You have Widthand Heightset to Auto, so I guess you was to allow the control to take as much space as needed but not more.
您已经Width并Height设置为Auto,所以我猜您是为了让控件根据需要占用尽可能多的空间,但不能更多。
Also, UserControlis not resizing by itself, but depends upon the layout that it's part of.
此外,UserControl它本身不会调整大小,而是取决于它所属的布局。
So, the quickest way to fix your issue would be to set HorizontalAlignment="Left"and VerticalAlignment="Top". But you should consider the whole layout of your application and how the UC is affected by-/affects on other components of the UI.
因此,解决问题的最快方法是设置HorizontalAlignment="Left"和VerticalAlignment="Top"。但是您应该考虑应用程序的整个布局以及 UC 如何受到/影响 UI 的其他组件。
回答by Jawahar
Then the Parent property of your UserControl is holding the Window instance. Most of times, it will be NavigationWindow. Try the below code in loaded event of your UserControl and it will work.
然后您的 UserControl 的 Parent 属性持有 Window 实例。大多数情况下,它将是 NavigationWindow。在 UserControl 的加载事件中尝试以下代码,它将起作用。
((NavigationWindow)this.Parent).ResizeMode = ResizeMode.NoResize

