WPF - 尝试根据其初始高度设置可调整大小窗口的 MinHeight

发布于 2024-10-10 11:33:24 字数 1010 浏览 0 评论 0原文

我试图根据其初始高度(当 SizeToContent="WidthAndHeight" 时)设置可调整大小窗口的 MinHeight / MinWidth。

我看到了几个答案/解决方案:

http://weblogs.asp。 net/psheriff/archive/2010/01.aspx

根据子属性设置表单MinWidth和MinHeight

但是:

  1. 我正在尝试使用MVVM模式 并希望能够实现 这在 xaml 中。

  2. 我也想保留价值观 例如 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:

  1. I am trying to use the MVVM pattern
    and would like to be able to achieve
    this in the xaml.

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘羡 2024-10-17 11:33:25

您的代码:

<Window
        ....
        x:Name="mainWindow"
        SizeToContent="WidthAndHeight" 
        ResizeMode="CanResizeWithGrip"
        MinHeight="{Binding ElementName=mainWindow, Mode=OneTime, Path=ActualHeight}"
>

它将不起作用,因为 ActualHeight 的默认值为零,并且当 WPF 框架调整窗口大小时,它已经为 MinHeight 分配了默认值ActualHeight 为零!

您可以尝试的第一件事是:将 Mode=OneTime 更改为 Mode=Default,以便 WPF 可以在 ActualHeight< 时更新 MinHeight /code> 在调整窗口大小时发生变化。如果这有效,那么你会很高兴。

否则,您必须处理 SizeChanged 事件,并在处理程序中更新 MinHeight

<Window
            ....
            x:Name="mainWindow"
            SizeToContent="WidthAndHeight" 
            ResizeMode="CanResizeWithGrip"
            SizeChanged="Window_SizeChanged"
 >

在后面的代码中:

bool firstTime= true;
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
     FrameworkElement  element = sender as FrameworkElement;
     if ( firstTime)
     {
            element.MinHeight = e.NewSize.Height;
            firstTime= false;
     }
}

希望它能解决您的问题。或者至少会给你一些如何继续的想法。如果您想固定窗口的大小,则还可以在 Window_SizeChanged() 处理程序中设置 MaxHeight


仅 XAML 解决方案

<Window
        x:Name="mainWindow"
        SizeToContent="WidthAndHeight" 
        ResizeMode="CanResizeWithGrip"
 >
 <Window.Triggers>
 <EventTrigger RoutedEvent="SizeChanged">
    <BeginStoryboard>
      <Storyboard Storyboard.TargetName="mainWindow">
            <DoubleAnimation Storyboard.TargetProperty="MinHeight" 
                             To="{Binding ElementName=mainWindow, Path=ActualHeight}"/>
       </Storyboard>
    </BeginStoryboard>
 </EventTrigger>
 </Window.Triggers>
 <!---- other code goes here--->
 </Window>

Your code:

<Window
        ....
        x:Name="mainWindow"
        SizeToContent="WidthAndHeight" 
        ResizeMode="CanResizeWithGrip"
        MinHeight="{Binding ElementName=mainWindow, Mode=OneTime, Path=ActualHeight}"
>

It will not work, because the default value of ActualHeight is zero, and by the time WPF framework resizes your window, it already have assigned MinHeight with the default value of ActualHeight which is zero!

First thing you can try is this: change Mode=OneTime to Mode=Default, so that WPF could update MinHeight when ActualHeight 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 the MinHeight.

<Window
            ....
            x:Name="mainWindow"
            SizeToContent="WidthAndHeight" 
            ResizeMode="CanResizeWithGrip"
            SizeChanged="Window_SizeChanged"
 >

In the code-behind:

bool firstTime= true;
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
     FrameworkElement  element = sender as FrameworkElement;
     if ( firstTime)
     {
            element.MinHeight = e.NewSize.Height;
            firstTime= false;
     }
}

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 the Window_SizeChanged() handler.


XAML ONLY SOLUTION

<Window
        x:Name="mainWindow"
        SizeToContent="WidthAndHeight" 
        ResizeMode="CanResizeWithGrip"
 >
 <Window.Triggers>
 <EventTrigger RoutedEvent="SizeChanged">
    <BeginStoryboard>
      <Storyboard Storyboard.TargetName="mainWindow">
            <DoubleAnimation Storyboard.TargetProperty="MinHeight" 
                             To="{Binding ElementName=mainWindow, Path=ActualHeight}"/>
       </Storyboard>
    </BeginStoryboard>
 </EventTrigger>
 </Window.Triggers>
 <!---- other code goes here--->
 </Window>
冷了相思 2024-10-17 11:33:25

我怀疑您正在寻找这段代码:

<Window 
x:Name="myWindow" 
MinHeight="266" Height="{Binding ElementName=myWindow, Path=MinHeight}" 
MinWidth="480"  Width="{Binding ElementName=myWindow, Path=MinWidth}">

这将高度和宽度设置为 MinHeight 和 MinWidth 集。所以你只能改变一个位置。

I suspect you are looking for this piece of code:

<Window 
x:Name="myWindow" 
MinHeight="266" Height="{Binding ElementName=myWindow, Path=MinHeight}" 
MinWidth="480"  Width="{Binding ElementName=myWindow, Path=MinWidth}">

This sets the Height and Width to the MinHeight and MinWidth set. So you only have one position to change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文