2013-06-20 21 views
0

我有一個列表框,我想在其中佈局項目。我正在努力獲得一個只與父對象一樣寬的stackpanel。也就是說,我在我的listitem中有一個textblock,它可以包含很多信息,並且我希望它可以根據父寬度來包裝或修剪(尚未確定哪個)。StackPanel - 限制爲父列表框的寬度

列表框項目是一個DataTemplate,但是爲了這篇文章的目的,我已經將它複製爲ListBox中的listboxitem。

這全部包含在一個頁面中。

<Grid>   
     <ListBox Name="ListBoxManageMedia" HorizontalContentAlignment="Stretch" Margin="10,52,10,41" ScrollViewer.VerticalScrollBarVisibility="Visible">   
      <ListBoxItem Height="70" Name="ListBoxItem" PreviewMouseDown="ListBoxItem_OnMouseDown">  
       <StackPanel Height="65" Orientation="Horizontal"> 
        <StackPanel Name="VideoImage2" Height="65" Width="102"/>  
         <StackPanel Name="VideoData2" HorizontalAlignment="Stretch" Height="65"> 
          <TextBlock Text="Title" FontWeight="Bold" FontSize="18"></TextBlock> 
          <TextBlock TextTrimming="None" Text="long test text about something or nothing to demonstrate the long description issues I'm having. I want this to wraplong test text about something or nothing to demonstrate the long description issues I'm having. I want this to wrap" TextWrapping="Wrap" FontSize="13" Margin="0,0,0,0"/> 
          <StackPanel Orientation="Horizontal"> 
           <TextBlock Text="Cat" FontSize="12" FontStyle="Italic"/> 
           <Border Width="50"></Border> 
           <TextBlock Text="Status" FontSize="12"/> 
          </StackPanel> 
         </StackPanel>     
       </StackPanel> 
      </ListBoxItem>  
     </ListBox>  
    </Grid> 

我怎樣才能讓我的文本塊(真正長的文本)只能和列表框一樣寬? (這'附加'的頁面邊緣罰款)

Width of child control should match width of parent container我已經試過這個SO回答沒有成功,和其他一些喜歡它。

編輯:

即使作爲一個網格我的長文只是運行在關閉的頁面。我在這裏做錯了什麼?

<ListBoxItem Height="70" HorizontalContentAlignment="Stretch" Margin="0,0,10,0" Name="ListBoxItem" PreviewMouseDown="ListBoxItem_OnMouseDown"> 
       <Grid Height="65" Margin="0,0,0,0"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="50*"/> 
         <ColumnDefinition Width="330*"/> 
         <ColumnDefinition Width="3*"/> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="23"></RowDefinition> 
         <RowDefinition Height="23"/> 
         <RowDefinition Height="19"/> 
        </Grid.RowDefinitions> 
        <TextBlock Grid.Column="1" Grid.Row="0" Text="Title" FontWeight="Bold" FontSize="18"></TextBlock> 
        <TextBlock Grid.Column="1" Grid.Row="1" DockPanel.Dock="Top" TextTrimming="CharacterEllipsis" Text="long test text about something or nothing to demonstrate the long description issues I'm having. I want this to wraplong test text about something or nothing to demonstrate the long description issues I'm having. I want this to wrap" TextWrapping="NoWrap" FontSize="13" Margin="0,0,0,0"/> 
        <StackPanel Grid.Column="1" Grid.Row="2" DockPanel.Dock="Top" Orientation="Horizontal"> 
         <TextBlock Text="Cat" FontSize="12" FontStyle="Italic"/> 
         <Border Width="50"></Border> 
         <TextBlock Text="Status" FontSize="12"/> 
        </StackPanel> 
       </Grid> 
      </ListBoxItem> 
+0

你真的需要'StackPanel'來佈置項目嗎? 「網格」會起作用嗎? – ChrisF

+0

因爲我鍵入我正在嘗試,現在沒有太大的成功,目前 – Damo

回答

0

好吧。

此:

ScrollViewer.HorizontalScrollBarVisibility="Disabled" 

和轉換StackPanels到DockPanels的伎倆。

0
<TextBlock Grid.Column="1" 
Width="{Binding ActualWidth, ElementName=ListBoxManageMedia}" Grid.Row="1" 
DockPanel.Dock="Top" TextTrimming="CharacterEllipsis" 
Text="long test text about something or nothing to demonstrate the long 
description issues I'm having. I want this to wraplong test text about something 
or nothing to demonstrate the long description issues I'm having. I want this to wrap" 
TextWrapping="NoWrap" FontSize="13" Margin="0,0,0,0"/> 

如果你想將TextBlock把其父的寬度,可以綁定像上面的寬度,但要確保它的父說列表框在這種情況下有其寬度定義。同樣的高度。 Height={Binding ActualHeight, ElementName=ListBoxManageMedia}

+0

謝謝@傑克我沒有關於綁定寬度的事情。 – Damo

+0

沒問題。很高興我能幫上忙。 – Jack