ListView中有多行文本

时间:2012-05-20 13:22:26

标签: c# wpf xaml listview listviewitem

我正在尝试在listView中获取多行我目前有以下XAML:

        <ListView x:Name="listTasks" Margin="0,0,1.5,0" Background="{x:Null}" BorderBrush="{x:Null}" BorderThickness="0" Foreground="White" ItemsSource="{Binding TasksCollection}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto">
        <ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                    <Trigger Property="IsSelected"
                                     Value="True">
                            <Setter Property="Background"
                                            Value="{x:Null}" />
                            <Setter Property="BorderBrush"
                                            Value="{x:Null}" />
                    </Trigger>
                    <Trigger Property="IsMouseOver"
                                     Value="True">
                            <Setter Property="Background"
                                            Value="{x:Null}" />
                            <Setter Property="BorderBrush"
                                            Value="{x:Null}" />
                    </Trigger>
            </Style.Triggers>
            <Setter Property="Height" Value="50px" />
            <Setter Property="Focusable" Value="false" />
    </Style>
</ListView.ItemContainerStyle>
        <ListView.Resources>
                <Style x:Key="CustomHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
                    <Setter Property="Visibility" Value="Collapsed" />
                </Style>
            </ListView.Resources>
            <ListView.View>
                <GridView ColumnHeaderContainerStyle="{DynamicResource CustomHeaderStyle}">
                    <GridViewColumn Header="">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock>
                                <TextBlock.Text>
                                    <MultiBinding  StringFormat=" Module: {0} Conditions: {1}|{2}|{3} for {4} minutes">
                                        <Binding Path="Module"/>
                                        <Binding Path="FirstValue"/>
                                        <Binding Path="SecondValue"/>
                                        <Binding Path="ThirdValue"/>
                                        <Binding Path="Minutes"/>
                                    </MultiBinding>
                                </TextBlock.Text>
                                </TextBlock>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

输出如下内容:

enter image description here

我想要实现的是这样的事情(忽略不同的彩色背景):

enter image description here

我似乎无法理解如何做到这一点,任何想法?

由于

1 个答案:

答案 0 :(得分:1)

您可以使用垂直StackPanel将一系列TextBlock元素放在另一个上面:

<DataTemplate>
   <StackPanel>
      <TextBlock Text="{Binding Module, StringFormat='Module: {0}'}"/>
      <TextBlock Text="{Binding FirstValue, StringFormat='Module: {0}'}"/>
      ...
   </StackPanel>
</DataTemplate>

或者您可以在一个`TextBlock'中格式化所有内容:

<DataTemplate>
   <TextBlock>
      <Run Text="Module: "/><Run Text="{Binding Module}"/><LineBreak/><Run Text="Conditions: "/><Run Text="{Binding FirstValue}"/><LineBreak/>
      ...
   </TextBlock>
</DataTemplate>