WPF Listview отображать несколько строк с шаблоном - PullRequest
0 голосов
/ 15 февраля 2011

В 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 Ответ

1 голос
/ 15 февраля 2011
  1. Может быть, вам не нужно ListView.ItemsControl по моему мнению является прямым эквивалентом Repeater

  2. Ваш ItemTemplate полностью испорчен - я считаю, что это вызывает ваши проблемыВместо того, чтобы помещать все в один Grid, вы должны использовать несколько (возможно, вложенных) DockPanels / StackPanels.Уберите все свои фантастические поля - это должно помочь.VS дизайнер плохо выполняет свою работу по созданию XAML, поэтому я предпочитаю редактировать XAML самостоятельно.

...