WPF中的DataTemplate:如何实现TextWrapping? [英] DataTemplate in WPF: How to achive TextWrapping?
问题描述
嘿XML专家在那里.
我在设计Window时遇到了一些问题.
我有一个ListBox,它的项目应获得以下模板:
[图片] [名称] [描述] [喜欢计数]
图片的宽度为56像素,名称为120,LikeCount为60像素.调整窗口大小时,说明应自动调整大小.这就是问题所在!它不包装,它使用所需的空间.我如何强迫它包起来?我的窗口的MinWidth为300,因此说明将始终可见...
Hey XML gurus out there.
I have a little problem designing my Window.
I have a ListBox and it''s items should get the following template:
[Picture][Name][Description][LikeCount]
The Picture is 56 pixels width, the name 120 and the LikeCount 60 pixels. The Description should be auto-resizing when I resize the window. And this is the problem! It doesn''t wrap, it uses as much space as it needs. How can I force it to wrap? My window has a MinWidth of 300, so the description will be always visable...
<Window x:Class="NeonMika.EightTracksPlayer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:track="clr-namespace:NeonMika.EightTracks;assembly=NeonMika.EightTracks"
xmlns:local="clr-namespace:NeonMika.EightTracksPlayer"
Title="MainWindow" Height="350" Width="525" MinWidth="300" Loaded="Window_Loaded">
<Window.Resources>
<Style x:Key="StretchedContainerStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</Window.Resources>
<Grid>
<ScrollViewer>
<StackPanel>
<ListBox Name="MixControl" HorizontalAlignment="Stretch" ItemContainerStyle="{StaticResource StretchedContainerStyle}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border CornerRadius="7" BorderThickness="2" BorderBrush="Black" HorizontalAlignment="Stretch">
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Margin="3" CornerRadius="5" BorderThickness="2" BorderBrush="Black" Width="56" Height="56" Grid.Column="0" VerticalAlignment="Center">
<Border.Background>
<ImageBrush ImageSource="{Binding BiggestSqCover}"/>
</Border.Background>
</Border>
<TextBlock Grid.Column="1" Margin="3" TextWrapping="Wrap" Width="120" Text="{Binding Name}" VerticalAlignment="Center"/>
<DockPanel Grid.Column="3" Margin="3" VerticalAlignment="Center" Width="60" >
<TextBlock TextWrapping="NoWrap" DockPanel.Dock="Top" Text="Liked " VerticalAlignment="Center" HorizontalAlignment="Center"/>
<TextBlock TextWrapping="NoWrap" DockPanel.Dock="Top" Text="{Binding LikesCount}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<TextBlock TextWrapping="NoWrap" DockPanel.Dock="Top" Text=" times" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</DockPanel>
<TextBlock Text="{Binding Description}" VerticalAlignment="Center" Grid.Column="2" TextWrapping="Wrap" Margin="3"></TextBlock>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</ScrollViewer>
</Grid>
</Window>
推荐答案
关键是限制水平滚动:
Hi... the key is to restrict the horizontal scrolling:
<ListBox Name="MixControl" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
这篇关于WPF中的DataTemplate:如何实现TextWrapping?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!