问题
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:

回答1:
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">
回答2:
I know this is several years after the fact, but I recently ran across this question as I was dealing with a similar problem I had. 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.
回答3:
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
回答4:
You can set the controls MinHeight property to prevent it getting smaller that desired.
回答5:
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.
回答6:
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?
来源:https://stackoverflow.com/questions/9319248/how-to-avoid-having-a-window-smaller-than-the-minimum-size-of-a-usercontrol-in-w