WPF - 尝试根据其初始高度设置可调整大小窗口的 MinHeight
我试图根据其初始高度(当 SizeToContent="WidthAndHeight" 时)设置可调整大小窗口的 MinHeight / MinWidth。
我看到了几个答案/解决方案:
http://weblogs.asp。 net/psheriff/archive/2010/01.aspx
但是:
我正在尝试使用MVVM模式 并希望能够实现 这在 xaml 中。
我也想保留价值观 例如 MinHeight 出 ViewModel - 我认为它们不属于 那里,因为他们系着一个微不足道的部分 视图的行为 视图模型。这是我想留给用户体验设计师的事情。
我正在努力解决的一个解决方案是使用以下 xaml/绑定:
<Window
....
x:Name="mainWindow"
SizeToContent="WidthAndHeight"
ResizeMode="CanResizeWithGrip"
MinHeight="{Binding ElementName=mainWindow, Mode=OneTime, Path=ActualHeight}"
>
我希望“Mode=OneTime”将 MinHeight 绑定到窗口高度的初始值。
但它不起作用..
有人可以解释一下为什么吗?有符合我的标准的解决方案吗?
谢谢,
马克
I am trying to set the MinHeight / MinWidth of a resizeable window based on its initial height (when SizeToContent="WidthAndHeight").
I have seen a couple of answers / solutions:
http://weblogs.asp.net/psheriff/archive/2010/01.aspx
Set form MinWidth and MinHeight based on child property
However:
I am trying to use the MVVM pattern
and would like to be able to achieve
this in the xaml.I would also like to keep values
such as MinHeight out of the
ViewModel - I dont think they belong
there as they tie a trivial part of
the behaviour of the view to the
viewmodel. This is something I would like to leave to the UX designer.
A solution I am struggling with is to use the following xaml / binding:
<Window
....
x:Name="mainWindow"
SizeToContent="WidthAndHeight"
ResizeMode="CanResizeWithGrip"
MinHeight="{Binding ElementName=mainWindow, Mode=OneTime, Path=ActualHeight}"
>
I would hope that 'Mode=OneTime' would bind the MinHeight to the initial value of the windows height.
But it does not work..
Can someone please explain why? Is there a solution that meets my criteria?
Thanks,
Mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码:
它将不起作用,因为
ActualHeight
的默认值为零,并且当 WPF 框架调整窗口大小时,它已经为MinHeight
分配了默认值ActualHeight
为零!您可以尝试的第一件事是:将
Mode=OneTime
更改为Mode=Default
,以便 WPF 可以在ActualHeight< 时更新
MinHeight
/code> 在调整窗口大小时发生变化。如果这有效,那么你会很高兴。否则,您必须处理
SizeChanged
事件,并在处理程序中更新MinHeight
。在后面的代码中:
希望它能解决您的问题。或者至少会给你一些如何继续的想法。如果您想固定窗口的大小,则还可以在
Window_SizeChanged()
处理程序中设置MaxHeight
。仅 XAML 解决方案
Your code:
It will not work, because the default value of
ActualHeight
is zero, and by the time WPF framework resizes your window, it already have assignedMinHeight
with the default value ofActualHeight
which is zero!First thing you can try is this: change
Mode=OneTime
toMode=Default
, so that WPF could updateMinHeight
whenActualHeight
gets changed on resizing the window. If that works, then you'll be happy.Otherwise, you've to handle the
SizeChanged
event, and in the handler you can update theMinHeight
.In the code-behind:
Hope it will solve your problem. Or atleast will give you some idea as to how to proceeed. If you want to fix the size of your window, then you can also set the
MaxHeight
in theWindow_SizeChanged()
handler.XAML ONLY SOLUTION
我怀疑您正在寻找这段代码:
这将高度和宽度设置为 MinHeight 和 MinWidth 集。所以你只能改变一个位置。
I suspect you are looking for this piece of code:
This sets the Height and Width to the MinHeight and MinWidth set. So you only have one position to change.