WPF Listview 使用模板显示多行
在 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>
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您不需要
ListView
。在我看来,ItemsControl
直接相当于Repeater
您的
ItemTemplate
完全搞砸了 - 我相信它会导致您的问题。您应该使用多个(可能是嵌套的)DockPanels
/StackPanels
,而不是将所有内容放入一个Grid
中。消除你所有的精彩利润——这应该会有帮助。 VS 设计师在创建 XAML 方面做得很糟糕,这就是为什么我更喜欢自己编辑 XAML。Maybe you do not need a
ListView
.ItemsControl
in my opinion is direct equivalent forRepeater
Your
ItemTemplate
is messed up completely - I believe it causes your problems. Instead of putting everything into oneGrid
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.