正如我们一直在 WPF 房间里讨论这个问题一样,这是一个看似简单的复杂问题。
如果我们在 mdiChild 上设置宽度/高度,我们最终会失去调整大小的功能。所以我已经发布了一个修复的要点,根据我们的聊天似乎已经解决了这个问题
所以为了获得大小限制,我使用了普通绑定(这个复制粘贴来自我的测试项目):
<Window x:Class="TestApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MDI="clr-namespace:WPF.MDI;assembly=WPF.MDI" xmlns:TestApplication="clr-namespace:TestApplication"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Title="MainWindow">
<MDI:MdiContainer Name="mainContainer">
<MDI:MdiChild x:Name="mdiChild" MaximizeBox="False" MinimizeBox="False" Resizable="True" ShowIcon="False" Width="{Binding Width, ElementName=childElement}" Height="{Binding Height, ElementName=childElement}">
<TestApplication:DatabaseTable x:Name="childElement"/>
</MDI:MdiChild>
</MDI:MdiContainer>
</Window>
这解决了主要的宽度和高度问题。
这段代码需要替换 WPF.MDI 库中的MdiChild.cs Changeset 81799 resize handler source(你可以用这个替换现有的相关代码):
/// <summary>
/// Handles the DragDelta event of the ResizeLeft control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Controls.Primitives.DragDeltaEventArgs"/> instance containing the event data.</param>
private void ResizeLeft_DragDelta(object sender, DragDeltaEventArgs e)
{
if (ActualWidth - e.HorizontalChange < MinWidth)
return;
double newLeft = e.HorizontalChange;
if (Position.X + newLeft < 0)
newLeft = 0 - Position.X;
Width = ActualWidth - newLeft;
Position = new Point(Position.X + newLeft, Position.Y);
if (sender != null)
Container.InvalidateSize();
}
/// <summary>
/// Handles the DragDelta event of the ResizeTop control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Controls.Primitives.DragDeltaEventArgs"/> instance containing the event data.</param>
private void ResizeTop_DragDelta(object sender, DragDeltaEventArgs e)
{
if (ActualHeight - e.VerticalChange < MinHeight)
return;
double newTop = e.VerticalChange;
if (Position.Y + newTop < 0)
newTop = 0 - Position.Y;
Height = ActualHeight - newTop;
Position = new Point(Position.X, Position.Y + newTop);
if (sender != null)
Container.InvalidateSize();
}
/// <summary>
/// Handles the DragDelta event of the ResizeRight control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Controls.Primitives.DragDeltaEventArgs"/> instance containing the event data.</param>
private void ResizeRight_DragDelta(object sender, DragDeltaEventArgs e)
{
if (ActualWidth + e.HorizontalChange < MinWidth)
return;
Width = ActualWidth + e.HorizontalChange;
if (sender != null)
Container.InvalidateSize();
}
/// <summary>
/// Handles the DragDelta event of the ResizeBottom control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Controls.Primitives.DragDeltaEventArgs"/> instance containing the event data.</param>
private void ResizeBottom_DragDelta(object sender, DragDeltaEventArgs e)
{
if (ActualHeight + e.VerticalChange < MinHeight)
return;
Height = ActualHeight + e.VerticalChange;
if (sender != null)
Container.InvalidateSize();
}