2012-08-10 108 views
1

我有一个应用程序,我在这里编写了自己的导航窗口(由于各种原因),并且内容位于边框内。当我换出内容时,我需要一种机制,一旦内容已经被渲染,通知我我可以自动调整整个窗口的大小。当边框内容发生变化时自动调整大小

我需要一个将窗口大小调整为内容或给出最大大小的行为。自动调整大小后,窗口应该以任何方式由用户调整大小。

我使用:

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; 
     } 
    } 
} 
+0

代码你看看http://msdn.microsoft.com/en-us/library/system.windows.window.contentrendered.aspx? – Dtex 2012-08-10 08:07:52

+0

当然可以。但是这个事件只会在第一次显示窗口时触发一次,而不会在内容发生变化时触发。 – metacircle 2012-08-10 08:19:20

+0

我刚刚检查过它,它为我工作。每当我更改窗口的内容时,该事件就会被触发 – Dtex 2012-08-10 09:16:03

回答

0

只是删除边框上的大小,只有经过设置this.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;InitializeComponent();

工作XAML:

<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" Height="350" Width="525" 
     ContentRendered="Window_ContentRendered"> 
    <StackPanel> 
     <Border x:Name="border" 
       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> 

背后

public partial class MainWindow : Window 
{ 
    int rendercount = 0; 
    Random rnd = new Random(); 

    public MainWindow() 
    { 
     InitializeComponent(); 

     this.SizeToContent = System.Windows.SizeToContent.WidthAndHeight; 
    } 

    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; 
    } 
} 
+0

我的主要问题是内容呈现事件只能在开始时触发一次,而不是在内容发生变化时(因此必须呈现)。 – metacircle 2012-08-16 08:25:25

相关问题