2013-12-14 47 views
0

我有以下XAML代碼:WPF TextBlock的擴張佈局內

<DataTemplate x:Key="TileViewDT"> 
     <DockPanel> 
      <StackPanel Height="40.5" DockPanel.Dock="Right" Margin="5,2,5,2" VerticalAlignment="Bottom"> 
       <TextBlock x:Name="Name" TextWrapping="Wrap" Text="{Binding [email protected]}" Width="132" /> 
       <TextBlock x:Name="Size" Foreground="DarkGray" TextWrapping="Wrap" Text="{Binding [email protected]}" /> 
      </StackPanel> 
      <Image x:Name="Img" Source="BtnImg/Computer.png" Stretch="Fill" Margin="10,0,5,0" Width="48" Height="48"/> 
     </DockPanel> 
    </DataTemplate> 

輸出:

enter image description here

我試圖讓Windows資源管理器相同的外觀一樣多儘可能,所以我想在TextBlocks的「名稱」和「大小」在文本短,當名稱文本的長度比項目空間長(它可能上漲)這個樣子:

enter image description here

所以,我怎麼會是能夠做到這一點?

回答

1

我覺得在StackPanel中,而不是設置高度應設置MaxHeight和VerticalAlignment屬性值應該是中心,而不是底部,應該做你的工作:

<DataTemplate x:Key="TileViewDT"> 
    <DockPanel> 
     <StackPanel MaxHeight="40.5" DockPanel.Dock="Right" Margin="5,2,5,2" VerticalAlignment="Center"> 
      <TextBlock x:Name="Name" TextWrapping="Wrap" Text="{Binding [email protected]}" Width="132" /> 
      <TextBlock x:Name="Size" Foreground="DarkGray" TextWrapping="Wrap" Text="{Binding [email protected]}" /> 
     </StackPanel> 
     <Image x:Name="Img" Source="BtnImg/Computer.png" Stretch="Fill" Margin="10,0,5,0" Width="48" Height="48"/> 
    </DockPanel> 
</DataTemplate> 

希望工程!

+0

完美工作,謝謝 –

+0

還有一個問題,當textBlock有2行如何減少第一行和第二行之間的空間? –

+1

將TextBlock LineStackingStrategy =「BlockLineHeight」和LineHeight屬性設置爲所需的任何值。 – user1246682