2011-08-14 52 views
3

我試圖模仿Windows 7中的粘滯便箋應用程序。在原始應用程序中,如果您將文本輸入便箋並且文本變得太大(垂直,如數字),以適應窗口,窗口會自動垂直擴展,一次一行,以留出更多空間。換句話說,在普通文本框中出現一個垂直滾動條並且文本向下滾動(以便第一行變爲不可見),在粘滯便箋中,文本框展開得足夠精確以適應文本,從而不出現滾動條。當然,當您手動調整窗口大小時,滾動條仍然會出現。文本太長時自動垂直擴展文本框

如果您有Windows 7,只需打開粘滯便箋應用程序並在粘滯便箋中鍵入幾行直到它放大。

我想模仿這種效果,但我沒有運氣。問題似乎是,實際的窗口應該調整大小,而不僅僅是文本框(我不認爲WPF以這種方式工作,調整子元素的大小可以'強制'父元素變得更大?至少不是一個窗口,對?)。

窗口在這一點上的內容是這樣的:

<Window Background="Transparent" BorderBrush="Transparent"> 
    <!-- Transparent border to draw dropshadow on --> 
    <Border Background="Transparent" BorderBrush="Transparent"> 
     <!-- Grid with UI elements --> 
     <Grid Margin="5" Background="Transparent"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="27" />  
       <RowDefinition Height="*" /> 
      </Grid.RowDefinitions> 

      <!-- Stickynote header --> 
      <Border ... /> 

      <!-- Content --> 
      <Border Grid.Row="1"> 

       <TextBox Text="{Binding ContentText}" ... /> 

      </Border> 
     </Grid> 
    </Border> 
</Window> 

有誰知道我怎麼可以達到這個效果?謝謝!

+0

嘗試設置寬度=「自動」爲文本框 – sll

回答

2

嘗試的窗口屬性SizeToContent="Height"

樣品

<Window ... 
     MaxHeight="500" 
     SizeToContent="Height"> 
    <Border Background="Transparent" BorderBrush="Transparent"> 
     <Grid Margin="5" Background="Transparent"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="27" /> 
       <RowDefinition Height="*" /> 
      </Grid.RowDefinitions> 
      <Border Grid.Row="1"> 
       <TextBox AcceptsReturn="True" MinHeight="100"/> 
      </Border> 
     </Grid> 
    </Border> 
</Window> 

編輯 爲了與TransparentWindow你貼在OnDragDelta添加transparentWindow.SizeToContent = SizeToContent.Manual(TransparentWindow.cs)使用它

private static void OnDragDelta(object sender, DragDeltaEventArgs e) 
{ 
    TransparentWindow transparentWindow = (TransparentWindow)sender; 
    Thumb thumb = e.OriginalSource as Thumb; 
    transparentWindow.SizeToContent = SizeToContent.Manual; 
    if (thumb != null && transparentWindow.WindowState == WindowState.Normal) 
    { 
     //... 
    } 
} 
+0

這似乎工作,但有一個主要問題:我不能再手動調整窗口大小現在(至少不是垂直)。這似乎是我的窗口有問題,我使用這個'透明'可調整大小的無邊界窗口代碼(我的窗口繼承了TransparentWindow):http://archive.msdn.microsoft.com/getwpfcodebywillhan/Release/ProjectReleases.aspx?ReleaseId= 1578 它可以正常工作,但由於某些原因,這個自定義的可調整大小的窗口將無法正常工作。是否因爲它可能手動設置高度屬性?我沒有線索... –

+1

更新了我的答案。對於該子類窗口,您需要在調整大小時將「SizeToContent」重置爲「手動」。試試吧,讓我知道,如果它給你想要的結果 –

+0

謝謝,這似乎可以工作。直到今天晚些時候我都無法測試,我會讓你知道的。 –