2017-05-24 110 views
1

我在用戶控件中有以下xaml代碼。WrapPanel不包裝內容

 <Grid> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="auto" /> 
       <ColumnDefinition Width="*" MinWidth="50"/> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="auto"/> 
      </Grid.RowDefinitions> 
      <ScrollViewer VerticalScrollBarVisibility="Auto" x:Name="scEmails"> 
       <ItemsControl Focusable="False" ItemTemplate="{StaticResource UserDataTemplate}"> 
        <ItemsControl.Items> 
         <system:String>123</system:String> 
         <system:String>123</system:String> 
         <system:String>123</system:String> 
         <system:String>123</system:String> 
         <system:String>12eee3</system:String> 
         <system:String>123eee</system:String> 
         <system:String>123fefef</system:String> 
        </ItemsControl.Items> 
        <ItemsControl.ItemsPanel> 
         <ItemsPanelTemplate> 
          <WrapPanel Orientation="Horizontal"/> 
         </ItemsPanelTemplate> 
        </ItemsControl.ItemsPanel> 
       </ItemsControl> 
      </ScrollViewer> 
      <TextBox Grid.Column="1" TextWrapping="NoWrap" Margin="0,0,0,0" VerticalAlignment="Center" PreviewKeyDown="txtAuto_PreviewKeyDown" MinWidth="50" /> 
     </Grid> 

在這裏我有一組項目要顯示在文本框的左側。渲染要求是:

  1. 如果沒有或很少的項目,讓項目佔據他們需要的空間儘可能多。
  2. 如果有太多的項目,應該換行到下一行並展開控件的Height,以確保文本框的寬度爲50或更多像素,除非這是不可能的(例如,有一個項目將使用太多空間)
  3. 如果行數太多(即超過MaxHeight屬性設置的限制),請顯示垂直滾動條。
  4. 在任何情況下,文本框應留下所有空間(在右側!)。

我使用ScrollViewer來實現#3,並使用WrapPanel來實現#2。但是上面的代碼並沒有給出預期的結果。在設計模式,它看起來像(項目模板是一個BorderTextBlock,should'nt這裏重要) design mode

,因爲它沒有包裝它不能滿足這個要求。

我能做些什麼來解決?

更新

如果我申請由Raviraj Palvankar答案的代碼,並刪除了所有項目,在設計模式佈局以下

enter image description here

然而,所需的輸出(根據要求項#4)是

enter image description here

在哪裏我的原代碼正確(雖然其他要求失敗)

+1

在ItemsControl中位於列有汽車,因爲它的寬度,提供MaxWidth或寬度您網格的第一列,那麼它應該工作。 –

+0

對。但由於這是一個用戶控件,MaxWidth或Width應該由其容器控制,而不是硬編碼。任何想法? –

+0

然後將第一列更改爲「*」,將另一列更改爲「自動」以使WrapPanel工作,您必須至少在容器內部定義固定寬度,否則將從哪裏包裝? –

回答

1

這裏是你的代碼沒有ItemTemplate,因此看起來像這樣。我添加了更多的字符串來表明它包裝。

<Grid Grid.Column="1" Grid.Row="3"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*" /> 
      <ColumnDefinition Width="Auto" MinWidth="50"/> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="auto"/> 
     </Grid.RowDefinitions> 
     <ScrollViewer VerticalScrollBarVisibility="Auto" x:Name="scEmails"> 
      <ItemsControl Focusable="False" > 
       <ItemsControl.Items> 
        <system:String>123</system:String> 
        <system:String>123</system:String> 
        <system:String>123</system:String> 
        <system:String>123</system:String> 
        <system:String>12eee3</system:String> 
        <system:String>123eee</system:String> 
        <system:String>123fefef</system:String> 
        <system:String>123</system:String> 
        <system:String>123</system:String> 
        <system:String>123</system:String> 
        <system:String>123</system:String> 
        <system:String>12eee3</system:String> 
        <system:String>123eee</system:String> 
        <system:String>123fefef</system:String> 
       </ItemsControl.Items> 
       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <WrapPanel Orientation="Horizontal"/> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
      </ItemsControl> 
     </ScrollViewer> 
     <TextBox Grid.Column="1" TextWrapping="NoWrap" Margin="0,0,0,0" VerticalAlignment="Center" PreviewKeyDown="txtAuto_PreviewKeyDown" MinWidth="50" /> 

Here is how it looks

+0

這樣,只需修復網格第一列的寬度,以便您的項目根據您的要求相應地包裝和修改其他寬度。 –

+0

這不符合要求。查看我的更新。 –

+0

我現在明白了這個問題。試圖想出點什麼,會盡快發佈。好問題 :) –