2016-05-24 99 views

回答

0

使用窗口的OnSizeChanged事件,

,做這樣的:

//get Screen's Width, Height 
private double screenHeight = SystemParameters.FullPrimaryScreenHeight; 
private double screenWidth = SystemParameters.FullPrimaryScreenWidth; 
private void MultiToolWindow_OnSizeChanged(object sender, SizeChangedEventArgs e) 
{ 
    //when window RightBoundary over screen 
    if (this.Left + this.Width > screenWidth) 
     this.Width = screenWidth-this.Left; //shrink the width 
    //when window DownBoundary over screen 
    if (this.Top + this.Height > screenHeight) 
     this.Height = screenHeight-this.Top; //shrink the height 
} 

注意,使用這個時候,窗口的SizeToContent屬性應該是在手冊,

如果不是,

,你可以這樣改:

public void SomeMethod(){ 

    //set to manual, this will invoke OnSizeChangedEvent at the same time but the shrink code won't work 
    this.SizeToContent = SizeToContent.Manual; 

    //this will invoke OnSizeChangedEvent and because now is manual the shrink code works 
    this.SizeToContent = SizeToContent.Manual; 


} 

做兩次,以確保在窗口的原始SizeToContent狀態WidthAndHeight生效過,

第一時間將其設置爲手動和收縮的代碼不會拿效果,

第二次導致狀態爲手動,因此縮水代碼將生效。