21

我无法绘制具有左对齐或居中对齐的列的列表视图。我查看了在这里或其他论坛上找到的一些解决方案,但它们似乎适用于所有专栏,或者我无法让它们正常工作。

到目前为止,我得到的最好的是这段代码,但所有内容都是左对齐的(我已将右对齐放入以测试代码)。有人可以告诉我哪里出错了吗?

<ListView Name="lsvQuestions" DockPanel.Dock="Bottom">                
     <ListView.View>
         <GridView>
             <GridViewColumn Width="450" Header="Question Text">
                 <GridViewColumn.CellTemplate>
                     <DataTemplate>
                         <TextBlock Text="{Binding QuestionText}" TextAlignment="Left"/>
                     </DataTemplate>
                 </GridViewColumn.CellTemplate>
                 </GridViewColumn>
                 <GridViewColumn Width="200" Header="Type">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding QuestionType}" TextAlignment="Right"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                 </GridViewColumn>
                 <GridViewColumn Width="100" Header="Page Number">
                     <GridViewColumn.CellTemplate>
                         <DataTemplate>
                             <TextBlock Text="{Binding QuestionPageNumber}" TextAlignment="Center"/>
                         </DataTemplate>
                     </GridViewColumn.CellTemplate>
                 </GridViewColumn>
                 <GridViewColumn Width="100" Header="Order">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding QuestionOrder}" TextAlignment="Center"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>               
    </ListView>
4

2 回答 2

54

默认情况下,您ListViewItem的 s 不会拉伸内容,这就是所有项目都显示在左侧的原因。添加这个,它会工作:

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
</ListView.ItemContainerStyle>
于 2012-07-09T15:29:50.540 回答
12

尽管LPL的提示已经避免了成千上万的麻烦,但我会更进一步,将Horizo​​ntalContentAlignmentVerticalContentAlignment定义为拉伸,以便针对大多数使用的表格驱动控件的预期对齐行为:

  • ListView 列标题和项目模板

  • DataGrid 列标题和单元格模板。

    <Window x:Class="TaskManager.UI.Views.AcumuladosPorTarefaWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:v="clr-namespace:TaskManager.UI.Views"
    xmlns:catel="http://catel.codeplex.com"        
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="AcumuladosPorTarefaWindow" Height="451.533" Width="323.305"
    >
    <Window.Resources>
    <Style TargetType="GridViewColumnHeader">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="VerticalContentAlignment" Value="Stretch" />
    </Style>
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="VerticalContentAlignment" Value="Stretch" />
    </Style>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="VerticalContentAlignment" Value="Stretch" />
    </Style>
    <Style TargetType="DataGridCell">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="VerticalContentAlignment" Value="Stretch" />
    </Style>
    </Window.Resources>
    <StackPanel Orientation="Vertical">
    
    <ListView ItemsSource="{Binding AcumuladosPorTarefa}" SelectedItem="{Binding LinhaSelecionada}">
        <ListView.View>
            <GridView>
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding TarefaID}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                    <GridViewColumnHeader>
                        <TextBlock Text="ID" TextAlignment="Center" />
                    </GridViewColumnHeader>
                </GridViewColumn>
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Tarefa.Nome}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                    <TextBlock Text="Nome" TextAlignment="Left"/>
                </GridViewColumn>
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding DuracaoInMS}" TextAlignment="Right" HorizontalAlignment="Stretch" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                    <GridViewColumnHeader Width="auto">
                        <TextBlock Text="Duração" TextAlignment="Right"/>
                    </GridViewColumnHeader>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
    <DataGrid ItemsSource="{Binding AcumuladosPorTarefa}" AutoGenerateColumns="False" SelectedItem="{Binding LinhaSelecionada}" CanUserAddRows="False" HorizontalAlignment="Stretch">
        <DataGrid.Columns>
            <DataGridTemplateColumn Width="auto" MinWidth="20" CanUserSort="True" CanUserReorder="True" CanUserResize="True">
                <DataGridTemplateColumn.Header>
                    <TextBlock Text="ID" TextAlignment="Right" />
                </DataGridTemplateColumn.Header>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding TarefaID}" HorizontalAlignment="Stretch"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Width="auto" MinWidth="150" CanUserSort="True">
                <DataGridTemplateColumn.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock Text="Nome" HorizontalAlignment="Left"/>
                    </DataTemplate>
                </DataGridTemplateColumn.HeaderTemplate>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Tarefa.Nome}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Duração" Width="auto" MinWidth="25" IsReadOnly="True">
                <DataGridTemplateColumn.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock Text="Duração" TextAlignment="Right"/>
                    </DataTemplate>
                </DataGridTemplateColumn.HeaderTemplate>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding DuracaoInMS}" TextAlignment="Right" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
    </StackPanel>
    </Window>
    

记住:您始终可以在应用程序级别定义默认样式和其他资源,因此您不必在每个视图中都定义它。

希望它可以帮助其他人,例如 LPL 的提示帮助我。

于 2014-11-20T13:16:26.377 回答