WPF Listview使用模板显示多行

时间:2011-02-15 19:31:18

标签: .net wpf-controls

在WPF中,我有一个绑定到数据集中的表的ListView。如果我使用(在完整代码视图中注释:

<ListView.View>
  <GridView>
  <GridViewColumn>

我显示了行,我可以看到多行并滚动其余行。

如果我尝试

<ListView.ItemTemplate>
  <DataTemplate>
     <Grid>
         <Grid.ColumnDefinitions>
           'Put a bunch of controls here bound to various columns in my data table

我一次只能看到一行/一行。我错过了其中一个控件的设置,或者ListView.ItemTemplate只能一次显示一条记录。

我在WPF中寻找的东西更像是一个Repeater控件,我可以为特定布局中的每个记录设置不同的控件。

完整的xaml:

<Window x:Class="GridTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="GridTest" Height="661" Width="750">

        <ListView Margin="4,1,8,1" 
                  Name="ListView_Transactions"  ItemsSource="{Binding}">
            <!--<ListView.View>
                <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Transactions">
                <GridViewColumn DisplayMemberBinding="{Binding Path=TransactionHeaderID}" />


            </GridView>

            </ListView.View>-->
            <ListView.ItemTemplate>
                    <DataTemplate>
                        <Grid MaxHeight="600">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" SharedSizeGroup="Key" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Label Content="{Binding Amount}" Height="25" Name="Label_Amount" VerticalAlignment="Top" Width="134" Margin="50,201,526,0" />
                            <Label Content="{Binding ActivityCategory}" Height="24" HorizontalAlignment="Left" Name="Label_ActivityCategory" VerticalAlignment="Top" Width="161" Margin="208,201,0,0" />
                            <TextBox Height="23" HorizontalAlignment="Left" Name="TextBox_Customer" VerticalAlignment="Top" Width="163" Margin="440,201,0,0" />
                            <Button Content="Breakout" Height="20" Name="Button_Breakout" Width="90" BorderThickness="0" ClickMode="Press" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="50,244,0,223">

                            </Button>
                            <Label Content="{Binding ShortDescription}" Height="27" HorizontalAlignment="Left" Name="Label_ShortDescription" VerticalAlignment="Top" Width="480" Margin="50,270,0,0" />
                            <TextBlock Height="65" HorizontalAlignment="Left" Name="TextBlock_LogDescription" Text="{Binding LongDescription}" VerticalAlignment="Top" Width="480" Margin="50,303,0,0" />

                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>

        </ListView>

</Window>

View of result, text does render normally, but smudged for security purposes

清理我的混乱:

<ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto">
        <ItemsControl Name="ItemsC" ItemsSource="{Binding}">         
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel HorizontalAlignment="Left">
                         <DockPanel >
                             <Label Content="{Binding Amount}" Height="25" DockPanel.Dock="Left" Name="Label_Amount" Width="100" />
                                <Label Content="{Binding ActivityCategory}" DockPanel.Dock="Left" Height="24" Name="Label_ActivityCategory" Width="100"  />
                                <TextBox Height="25" DockPanel.Dock="Left"  Name="TextBox_Customer" Width="123"  />
                            </DockPanel>
                            <StackPanel HorizontalAlignment="Left">
                                <Button Content="Breakout"  Height="25" Name="Button_Breakout" Width="90" HorizontalAlignment="Left" VerticalAlignment="Center" />
                                <Label Content="{Binding ShortDescription}" Name="Label_ShortDescription" Width="550" />
                                <TextBlock Height="200" Name="TextBlock_LogDescription" Text="{Binding LongDescription}" Width="550"  />
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
</ScrollViewer>

1 个答案:

答案 0 :(得分:1)

  1. 也许您不需要ListView。我认为ItemsControlRepeater

  2. 直接相同
  3. 你的ItemTemplate完全搞砸了 - 我相信这会导致你的问题。您应该使用多个(可能是嵌套的)Grid / DockPanels,而不是将所有内容放在一个StackPanels中。删除所有奇妙的利润 - 它应该有所帮助。 VS设计师很难创建一个XAML,这就是我喜欢自己编辑XAML的原因。