none
silverlight 中childwindow为什么不执行this.Width = Application.Current.Host.Content.ActualWidth ?? RRS feed

  • 问题

  • 在childwindow中,我想实现此功能:
    用户随意调整浏览器窗口大小,都会让childwindow全屏(跟随浏览器大小动态调整大小),所以我写了以下代码:

    public MyWindow()
     {
                InitializeComponent();

                this.Width = Application.Current.Host.Content.ActualWidth;
                this.Height = Application.Current.Host.Content.ActualHeight;

                Application.Current.Host.Content.Resized += Content_Resized;
    }

    void Content_Resized(object sender, EventArgs e)
    {
                this.Width = Application.Current.Host.Content.ActualWidth;
                this.Height = Application.Current.Host.Content.ActualHeight;
    }

    但我发现上面的代码却不能执行:调整浏览器大小,childwindow并不会自动调整大小,

    后来进过反复测试发现:

    代码1:this.Width = Application.Current.Host.Content.ActualWidth;
    代码2:this.Width = Application.Current.Host.Content.ActualWidth + 1;
    代码3:this.Width = Application.Current.Host.Content.ActualWidth + 1 - 1;

    其中代码1不能达到效果,代码2可以达到效果,代码3不能达到效果,何故????

    2013年5月27日 6:01

答案

  • 你好,

    请尝试设置 HorizontalAlignment 和VerticalAlignment值为 Stretch,如下,

    <controls:ChildWindow x:Class="SilverlightApplication4.ChildWindow1"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
               xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
               Title="ChildWindow1"
               HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

    后台代码如下:

    public ChildWindow1()
    {
      InitializeComponent();
    
      UpdateSize( null, EventArgs.Empty );
    
      App.Current.Host.Content.Resized += UpdateSize;
    }
    
    protected override void OnClosed( EventArgs e )
    {
      App.Current.Host.Content.Resized -= UpdateSize;
    }
    
    private void UpdateSize( object sender, EventArgs e )
    {
      this.Width = App.Current.Host.Content.ActualWidth;
      this.Height = App.Current.Host.Content.ActualHeight;
      this.UpdateLayout();
    }

    以下链接是以相似问题,请参考:

    http://stackoverflow.com/questions/10015145/childwindow-width-height-not-binding-correctly

    希望对您有所帮助。谢谢!


    Yanping Wang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年5月28日 6:27
    版主

全部回复

  • 你好,

    请尝试设置 HorizontalAlignment 和VerticalAlignment值为 Stretch,如下,

    <controls:ChildWindow x:Class="SilverlightApplication4.ChildWindow1"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
               xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
               Title="ChildWindow1"
               HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

    后台代码如下:

    public ChildWindow1()
    {
      InitializeComponent();
    
      UpdateSize( null, EventArgs.Empty );
    
      App.Current.Host.Content.Resized += UpdateSize;
    }
    
    protected override void OnClosed( EventArgs e )
    {
      App.Current.Host.Content.Resized -= UpdateSize;
    }
    
    private void UpdateSize( object sender, EventArgs e )
    {
      this.Width = App.Current.Host.Content.ActualWidth;
      this.Height = App.Current.Host.Content.ActualHeight;
      this.UpdateLayout();
    }

    以下链接是以相似问题,请参考:

    http://stackoverflow.com/questions/10015145/childwindow-width-height-not-binding-correctly

    希望对您有所帮助。谢谢!


    Yanping Wang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年5月28日 6:27
    版主
  • 多谢,我知道了,加上this.UpdateLayout();这一句就对了:

    void Content_Resized(object sender, EventArgs e)
    {
                this.Width = Application.Current.Host.Content.ActualWidth;
                this.Height = Application.Current.Host.Content.ActualHeight;

    this.UpdateLayout();
    }

    2013年6月9日 5:53