2011-07-26 72 views
1

我正在構建一個ListView,需要有五列 - 第一個需要具有可以是任何長度的文本,並且需要在窗口大小發生變化時進行換行(除了更改行高以便包裹的文本是可見的),另外四列是45的靜態寬度。我一直在尋找這個小時,我遇到的每個解決方案要麼需要靜態寬度要麼不工作。WPF ListView TextBlock TextWrapping

解決方案的嘗試:

汽車的列寬,1 *,2 *,等。(設置忽略) DockPanel中(設置忽略) WrapPanel(忽略) 設置寬於母公司的RelativeSource爲ActualWidth的(忽略)

任何想法?似乎有相當多的人有這個相同的問題,但我非常喜歡不必爲此列使用靜態寬度路由。特別是因爲當我這樣做時,內容會被切斷(即使對於該行,高度=「自動」)。整個窗口的寬度可以小到1024,但也可以是1600+,這就是爲什麼我需要動態調整大小的原因。這樣一來,較小的屏幕就可以進行內容包裝,而較大的屏幕將顯示內容適合的一行。

這裏是XAML:

<ListView.ItemTemplate> 
    <DataTemplate> 
     <Grid> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition /> 
       <ColumnDefinition Width="45" /> 
       <ColumnDefinition Width="45" /> 
       <ColumnDefinition Width="45" /> 
       <ColumnDefinition Width="45" /> 
      </Grid.ColumnDefinitions> 

      <!-- This is the TextBlock that needs to wrap its content (and 
       change the height of the row (so the full content is still 
       visible) to whatever the available space is, but should not 
       make overall ListView wider than the parent's width. --> 
      <TextBlock Text="{Binding Content}" Padding="20,6,6,6" />        

      <!-- These four blocks will have other content eventually, but only need 
       to be 45 wide --> 
      <TextBlock Text="X" Grid.Column="1" HorizontalAlignment="Center" /> 
      <TextBlock Text="X" Grid.Column="2" HorizontalAlignment="Center" /> 
      <TextBlock Text="X" Grid.Column="3" HorizontalAlignment="Center" /> 
      <TextBlock Text="X" Grid.Column="4" HorizontalAlignment="Center" /> 
     </Grid> 
    </DataTemplate> 
</ListView.ItemTemplate> 

回答

1

不是那麼容易...但它可以做到。

我爲你寫了一個解決方案。簡而言之,使用Expression Blend創建ListView模板的副本並刪除圍繞ItemPresenter的ScrollViewer。

這裏有一個更深入的解釋:

How to have the TextBlock in a left column of a Grid in a ListView Template expand or shrink with text wrapping?

+0

這正是我所期待的!我遇到的唯一問題是希望很容易解決,即文本包裝內容所在行的大小不會更改,因此包裝的內容會消失。我可以看到大多數角色的頂部,但不足以閱讀它們。感謝您讓我99%,但! – RubyHaus

+0

在我的盒子裏,我沒有看到與我的測試應用程序。您必須在應用中明確設置高度或將VerticalAlignment設置爲拉伸之外的其他值。在評論中發佈您的新XAML。 – Rhyous

+0

啊 - 就是這樣。有一個高度值爲30的ListView.ItemContainerStyle。我把它取下來,這件事情的作品正是我想要的。如果有一種方法可以給你多個upvote,我會。 – RubyHaus

-1

我想補充TextWrapping = 「包裝」 到第一TextBlock元素。

+0

它已添加,但在我的亂投醫我必須嘗試意外刪除它。但該設置被忽略。 – RubyHaus