How to disable resizing of user control in WPF

梦想与她 提交于 2020-01-15 08:14:34

问题


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"

.But it is not available in UserControl.Any suugestion?


回答1:


You have Width and Height set to Auto, so I guess you was to allow the control to take as much space as needed but not more.

Also, UserControl is not resizing by itself, but depends upon the layout that it's part of.

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.




回答2:


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.

((NavigationWindow)this.Parent).ResizeMode = ResizeMode.NoResize


来源:https://stackoverflow.com/questions/21306082/how-to-disable-resizing-of-user-control-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!