根据子属性设置表单MinWidth和MinHeight [英] Set form MinWidth and MinHeight based on child property
问题描述
我正在使用MVVM模式在WPF中编写应用程序.在我的应用程序中,我有一个IPopupWindowService
,我用它来创建一个弹出对话框窗口.
I'm writing an application in WPF using the MVVM pattern. In my application I've got an IPopupWindowService
which I use to create a popup dialog window.
要在弹出窗口中显示ViewModel,您需要执行以下操作:
So to show a ViewModel in a popup window you'd do something like this:
var container = ServiceLocator.Current.GetInstance<IUnityContainer>();
var popupService = container.Resolve<IPopupWindowService>();
var myViewModel = container.Resolve<IMyViewModel>();
popupService.Show((ViewModelBase)myViewModel);
这一切都很好.我想做的是能够将View
上的MinHeight
和MinWidth
设置为绑定到myViewModel
,并让弹出窗口使用这些设置,以便用户不能使窗口小于其内容所允许的范围. .在用户缩小窗口的那一刻,内容停止调整大小,但窗口没有调整大小.
This is all well and good. What I want to do is be able to set the MinHeight
and MinWidth
on the View
bound to myViewModel
and have the popup window use those settings so that a user cannot make the window smaller than its contents will allow. At the moment when the user shrinks the window the contents stops resizing but the window doesn't.
我将View映射到ResourceDictionarys中的ViewModel上是这样的:
I map my Views to my ViewModels in ResourceDictionarys like so:
<DataTemplate DataType="{x:Type ViewModels:MyViewModel}">
<Views:MyView />
</DataTemplate>
我的弹出视图如下:
<Window x:Class="TheCompany.Cubit.Shell.Views.PopupWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
SizeToContent="WidthAndHeight"
WindowStartupLocation="CenterOwner">
<DockPanel x:Name="panelContent">
<StackPanel HorizontalAlignment="Right" DockPanel.Dock="Bottom" Orientation="Horizontal" Visibility="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=ButtonPanelVisibility}">
<Button Width="75" IsDefault="True" x:Uid="ViewDialog_AcceptButton" Click="OnAcceptButtonClick" Margin="4">OK</Button>
<Button Width="75" IsCancel="True" x:Uid="ViewDialog_CancelButton" Click="OnCancelButtonClick" Margin="0,4,4,4">Cancel</Button>
</StackPanel>
<ContentPresenter Content="{Binding}" />
</DockPanel>
推荐答案
您可以在ViewModel上定义MinHeight
和MinWidth
属性,并使用数据绑定将View绑定到XAML中的那些属性:
You can define MinHeight
and MinWidth
properties on your ViewModel and use databinding to bind the View to those properties in XAML:
<...
MinHeight="{Binding Path=MinHeight}"
MinWidth="{Binding Path=MinWidth}"
.../>
这篇关于根据子属性设置表单MinWidth和MinHeight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!