我有一个应用程序,我在其中编写了自己的导航窗口(由于各种原因),并且内容托管在边框内。当我换出内容时,我需要一种机制,在内容呈现后立即通知我,以便我可以自动调整整个窗口的大小。
我需要一种将窗口大小调整为内容或给定最大大小的行为。自动调整大小后,用户应该以任何方式调整窗口大小。
我在用:
this.MaxHeight = 700;
this.MaxWidth = 800;
this.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
this.SizeToContent = System.Windows.SizeToContent.Manual;
this.MaxWidth = double.PositiveInfinity;
this.MaxHeight = double.PositiveInfinity;
但由于渲染并不总是完成,这并不总是有效。
编辑:我写了这个示例来显示渲染的内容只被触发一次:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="525"
Height="350"
ContentRendered="Window_ContentRendered">
<StackPanel>
<Border x:Name="border"
Width="200"
Height="200"
BorderBrush="Cornsilk"
BorderThickness="1">
<Ellipse Width="40"
Height="20"
Fill="AliceBlue"
Stroke="Black" />
</Border>
<Button x:Name="btn"
Click="btn_Click"
Content="ClickMe" />
<TextBlock x:Name="txtRender" />
</StackPanel>
</Window>
代码背后:
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
int rendercount = 0;
Random rnd = new Random();
public MainWindow()
{
InitializeComponent();
}
private void Window_ContentRendered(object sender, EventArgs e)
{
rendercount++;
txtRender.Text = rendercount.ToString();
}
private void btn_Click(object sender, RoutedEventArgs e)
{
Ellipse ell = new Ellipse();
ell.Width = rnd.Next(50, 100);
ell.Height = rnd.Next(10, 100);
ell.Stroke = Brushes.Black;
ell.StrokeThickness = 2;
border.Child = ell;
}
}
}