如何避免 WPF 中的 Window 小于 UserControl 的最小尺寸?

发布于 2025-01-06 13:42:33 字数 230 浏览 1 评论 0原文

我有一个用户控件(状态栏),它具有隐式最小尺寸(不是通过属性设置,我的意思是,当它达到最小尺寸时,它不能减小并且会被裁剪)。

有没有办法让主窗口知道 UserControl 将被裁剪并且不允许它减小其大小?对于 WPF 这样的智能布局系统,它必须有一种知道它正在裁剪内容的方式。看下一张图片:

在此处输入图片描述

I have an usercontrol (a statusbar) that has an implicit minimum size (not set via the property, what I mean is that when it reaches a minimum size it can not be reduced and it's cropped).

Is there a way to let the main window know that the UserControl will being to be cropped and don't allow it to reduce its size? With such a smart layout system as WPF has it must be a way that it knows that it's cropping things. Look at the next image:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

伪心 2025-01-13 13:42:33

部分中包含 MinHeight="MinSize" MinWidth="MinSize"

MinSize = Desired Integer价值 400/350 等

<Window x:Class="IOTA_WPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" 
    WindowState="Maximized"
    MinHeight="400" MinWidth="650">

Include MinHeight="MinSize" MinWidth="MinSize" inside the <Window> section

MinSize = Desired Integer Value ex 400/350 etc.

<Window x:Class="IOTA_WPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" 
    WindowState="Maximized"
    MinHeight="400" MinWidth="650">
dawn曙光 2025-01-13 13:42:33

我就是这样解决的。该解决方案特定于我的问题,但可以进行更改以获得各种所需的调整大小行为。

首先设置窗口,使其初始大小适合其内容。

<Window ...
    SizeToContent="WidthAndHeight" >

接下来,我设置每个嵌入式控件,以便它报告所需的宽度和高度。大多数内置控件都会为您执行此操作。您可以通过重写 MeasureOverride 对自定义控件执行此操作,也可以仅设置控件的宽度和高度。将此设置为您希望控件获得的最小尺寸。

<MyControl Name="_MyControlName" Width="640" Height="480" />

不必担心这些“硬”编码值会影响控件的行为。我们接下来处理这个问题。现在,当窗口显示时,它会自动调整以适应您的控件。

接下来订阅窗口加载事件。

<Window ...
    SizeToContent="WidthAndHeight" Loaded="Window_Loaded" >

在窗口加载事件中,您可以调整窗口及其中的控件的大小调整行为。您可以这样做,以便您的控件可以调整大小。您可以将窗口的最小尺寸设置为刚刚布置的尺寸。这里的优点是您只需让布局系统为您找到一个好的布局。你想利用它的工作。

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        // We know longer need to size to the contents.
        ClearValue(SizeToContentProperty);
        // We want our control to shrink/expand with the window.
        _MyControlName.ClearValue(WidthProperty);
        _MyControlName.ClearValue(HeightProperty);
        // Don't want our window to be able to get any smaller than this.
        SetValue(MinWidthProperty, this.Width);
        SetValue(MinHeightProperty, this.Height);
    }

您在 Window_Loaded 方法中放置的内容将取决于您想要完成的行为。

希望这可以帮助其他人节省时间。

This is how I solved it. The solution is specific to my problem, but can be altered to get a variety of desired resizing behaviors.

First set the window so that it initially sizes itself to fit its contents.

<Window ...
    SizeToContent="WidthAndHeight" >

Next I set each embedded control so that it reports a desired width and height. Most built in controls do this for you. You can do it for custom controls by overriding MeasureOverride or you can just set a width and height for the control. Make this the minimum size you want the control to get.

<MyControl Name="_MyControlName" Width="640" Height="480" />

Don't worry about these 'hard' coded values effecting the behavior of the control. We deal with that next. Now when the window is displayed it will automatically adjust itself to fit your controls.

Next subscribe to the window loaded event.

<Window ...
    SizeToContent="WidthAndHeight" Loaded="Window_Loaded" >

In the window loaded event you can adjust the sizing behavior of your window and the controls in it. You can make it so your controls can resize. You can set a minimum size for the window to the size you just had layed out. The advantage here is that you just allowed the layout system to find a good layout for you. You want to leverage its work.

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        // We know longer need to size to the contents.
        ClearValue(SizeToContentProperty);
        // We want our control to shrink/expand with the window.
        _MyControlName.ClearValue(WidthProperty);
        _MyControlName.ClearValue(HeightProperty);
        // Don't want our window to be able to get any smaller than this.
        SetValue(MinWidthProperty, this.Width);
        SetValue(MinHeightProperty, this.Height);
    }

What you place in the Window_Loaded method will depend on the behavior you are trying to accomplish.

Hope this can help some others save time.

再可℃爱ぅ一点好了 2025-01-13 13:42:33

您可以设置控件的 MinHeight 属性以防止其变得小于所需值。

You can set the controls MinHeight property to prevent it getting smaller that desired.

月寒剑心 2025-01-13 13:42:33

我知道我有点晚了,但我想说我已经找到了这个问题几乎最干净的解决方案。

1.) 创建自定义 UserControl(如果您正在使用任何)

public class ExtendedUserControl : UserControl
{
    public static readonly RoutedEvent ContentChangedEvent
    = EventManager.RegisterRoutedEvent(
        "ContentChanged",
        RoutingStrategy.Bubble,
        typeof(RoutedEventHandler),
        typeof(ExtendedUserControl));

    public event RoutedEventHandler ContentChanged
    {
        add { AddHandler(ContentChangedEvent, value); }
        remove { RemoveHandler(ContentChangedEvent, value); }
    }

    protected override void OnContentChanged(object oldContent, object newContent)
    {
        base.OnContentChanged(oldContent, newContent);
        RaiseEvent(new RoutedEventArgs(ContentChangedEvent, this));
    }
}

2.) 将事件附加到您的 UserControl

<extended:ExtendedUserControl Height="auto" Width="auto" cal:View.Model="{Binding CurrentModel}" ContentChanged="ExtendedUserControl_ContentChanged"/>

3.) 更新 现在我们进入解决方案的最后一部分。

private void ExtendedUserControl_ContentChanged(object sender, System.Windows.RoutedEventArgs e)
    {
        Thread task = new Thread(() =>
        {
            Dispatcher.Invoke(() =>
            {
                SizeToContent = SizeToContent.WidthAndHeight;
                MinHeight = Height;
                MinWidth = Width;
            });
        });
        task.Start();
    }

干杯

I know that I'm a little late but I would tell that I've found nearly the cleanest solution for this issue.

1.) Create a custom UserControl (if you are using any)

public class ExtendedUserControl : UserControl
{
    public static readonly RoutedEvent ContentChangedEvent
    = EventManager.RegisterRoutedEvent(
        "ContentChanged",
        RoutingStrategy.Bubble,
        typeof(RoutedEventHandler),
        typeof(ExtendedUserControl));

    public event RoutedEventHandler ContentChanged
    {
        add { AddHandler(ContentChangedEvent, value); }
        remove { RemoveHandler(ContentChangedEvent, value); }
    }

    protected override void OnContentChanged(object oldContent, object newContent)
    {
        base.OnContentChanged(oldContent, newContent);
        RaiseEvent(new RoutedEventArgs(ContentChangedEvent, this));
    }
}

2.) Attach the event to your UserControl

<extended:ExtendedUserControl Height="auto" Width="auto" cal:View.Model="{Binding CurrentModel}" ContentChanged="ExtendedUserControl_ContentChanged"/>

3.) UPDATED Now we come to the last part of the solution.

private void ExtendedUserControl_ContentChanged(object sender, System.Windows.RoutedEventArgs e)
    {
        Thread task = new Thread(() =>
        {
            Dispatcher.Invoke(() =>
            {
                SizeToContent = SizeToContent.WidthAndHeight;
                MinHeight = Height;
                MinWidth = Width;
            });
        });
        task.Start();
    }

Cheers

少女情怀诗 2025-01-13 13:42:33

您可以将项目放入包含组成部分的行的网格中吗?

所有必须具有最小高度的项目都可以排成具有相似最小高度的行。

添加最小高度 up = Window.MinHeight。因此,窗口永远不能小于必要行的集体最小高度,因此这些行应始终显示。

Can you put items in a Grid with Rows containing the constituent parts?

All items that must have a minimum height can go in rows with a similar minimum height.

Add the minimum heights up = Window.MinHeight. Thus the window can never be smaller than the collective minimum heights of the necessary rows, so those rows should always display.

離殇 2025-01-13 13:42:33

我想我记得在某处读过(我可能是错的),堆栈面板具有基于无穷大的边框,并且无法处理此类尺寸问题。

来自http://msdn.microsoft.com/en-us/library/ms754152。 虽然

DockPanel 与 StackPanel

DockPanel 也可以“堆叠”子元素,但 DockPanel 和 StackPanel 在某些使用场景中不会产生类似的结果。例如,子元素的顺序可以影响它们在 DockPanel 中的大小,但不会影响 StackPanel 中的大小。这是因为 StackPanel 测量的是 PositiveInfinity 的堆叠方向,而 DockPanel 仅测量可用大小。

帖子中有一部分也讨论了嵌套面板。这可能就是您所需要的。

或者也许使用状态位于底行的网格?

I think I remember reading somewhere (I may be wrong) that stack panels have borders based on infinity, and cannot handle such sizing issues.

From http://msdn.microsoft.com/en-us/library/ms754152.aspx

DockPanel vs StackPanel

Although DockPanel can also "stack" child elements, DockPanel and StackPanel do not produce analogous results in some usage scenarios. For example, the order of child elements can affect their size in a DockPanel but not in a StackPanel. This is because StackPanel measures in the direction of stacking at PositiveInfinity, whereas DockPanel measures only the available size.

There's a section of the post where it talks about nested panels too. Which might be what you need.

Or maybe use a grid with the status in the bottom row?

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