在使用Prism的MVVM应用程序中,子视图和viewmodel被加载到父视图UserControl“ ContentRegion”中
并且父视图具有固定大小
Height="868"
Width="1024"
MinHeight="868"
MinWidth="1024"
如何从子视图模型调整父视图大小
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:prism="http://prismlibrary.com/"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
prism:ViewModelLocator.AutoWireViewModel="True"
Height="868"
Width="1024"
MinHeight="868"
MinWidth="1024">
<Grid>
<UserControl prism:RegionManager.RegionName="ContentRegion" />
</Grid>
</Window>```
我不建议尝试从视图调整父窗口的大小,因为它不知道其位置。
尝试与SizeToContent一起玩。
如果确实需要手动调整大小,则可以使用Prism的IEventAggregator,并使用PubSubEvent<T>
进行Publish()和Subscribe()事件。在View的上下文中,您需要Publish()事件。然后在Window上订阅该事件并处理大小参数。
伪代码:
public class ResizeParentWindowEvent : PubSubEvent<YourSizeInfo> { }
eventAggregator.GetEvent<ResizeParentWindowEvent>().Publish(your_size_info_object);