WPF Listview 使用模板显示多行

发布于 2024-10-17 19:42:08 字数 4716 浏览 2 评论 0原文

在 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>

结果视图,文本确实正常渲染,但出于安全目的而被弄脏

清理了我的混乱:

<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>

In WPF I have a ListView that is bound to a table in a Dataset. If I use (Commented out in full code view:

<ListView.View>
  <GridView>
  <GridViewColumn>

I get the rows displayed and I can see multiple rows and scroll through the rest.

If I try

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

I can only see one row/record at one time. Am I missing a setting on one of the controls or is the ListView.ItemTemplate only going to show one record at a time.

I'm looking for something in WPF that is more like a Repeater control where I can have different controls for each record in a particular layout.

Full 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

Cleaned Up My Mess:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸵鸟症 2024-10-24 19:42:08
  1. 也许您不需要ListView。在我看来,ItemsControl 直接相当于 Repeater

  2. 您的 ItemTemplate 完全搞砸了 - 我相信它会导致您的问题。您应该使用多个(可能是嵌套的)DockPanels/StackPanels,而不是将所有内容放入一个 Grid 中。消除你所有的精彩利润——这应该会有帮助。 VS 设计师在创建 XAML 方面做得很糟糕,这就是为什么我更喜欢自己编辑 XAML。

  1. Maybe you do not need a ListView. ItemsControl in my opinion is direct equivalent for Repeater

  2. Your ItemTemplate is messed up completely - I believe it causes your problems. Instead of putting everything into one Grid you should use several (probably nested) DockPanels/StackPanels. Remove all your fantastic margins - it should help. VS designer does his job badly creating a XAML, that's why I prefer editing XAML on my own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文