[英]How can i create a ListView of specific no. of columns in WPF?
I want to create a ListView with 3 columns, i am creating a GridView inside ListView tag, but this is not providing me the same results i want. 我想创建一个包含3列的ListView,我在ListView标签中创建一个GridView,但这并没有为我提供我想要的相同结果。
Attached is the picture i want as result. 随附的是我想要的图片。
This is the piece of code i am using. 这是我正在使用的一段代码。
[![<ListView Name="List1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,70,0,0" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" Background="Transparent" BorderThickness="0" RenderTransformOrigin="-1.33,-0.562" SelectionChanged="ListView_SelectionChanged">
<ListView.View>
<GridView ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}">
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<DockPanel HorizontalAlignment="Right" Height="200" Width="200" LastChildFill="False" VerticalAlignment="Top">
<StackPanel Orientation="Vertical">
<Grid>
<Label Content="{Binding username}" Width="100" Height="200" VerticalAlignment="Bottom" HorizontalAlignment="Center"/>
<Image Source="{Binding imageSource}" Width="100" Height="100"/>
</Grid>
</StackPanel>
</DockPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<DockPanel HorizontalAlignment="Right" Height="200" Width="200" LastChildFill="False" VerticalAlignment="Top">
<StackPanel Orientation="Vertical">
<Grid>
<Label Content="{Binding username}" Width="100" Height="200" VerticalAlignment="Bottom" HorizontalAlignment="Center"/>
<Image Source="{Binding imageSource}" Width="100" Height="100"/>
</Grid>
</StackPanel>
</DockPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<DockPanel HorizontalAlignment="Right" Height="200" Width="200" LastChildFill="False" VerticalAlignment="Top">
<StackPanel Orientation="Vertical">
<Grid>
<Label Content="{Binding username}" Width="100" Height="200" VerticalAlignment="Bottom" HorizontalAlignment="Center"/>
<Image Source="{Binding imageSource}" Width="100" Height="100"/>
</Grid>
</StackPanel>
</DockPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>][1]][1]
The better way to do this is by using a UniformGrid
as the ItemsPanel
of the ListView
. 更好的方式来做到这一点是通过使用
UniformGrid
作为ItemsPanel
中的ListView
。
<ListView>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
...
</ListView>
Here is an example of doing this with a ListBox
instead of a ListView
, as discussed in the comments. 下面是使用
ListBox
而不是ListView
执行此操作的示例,如注释中所述。 This is the way I would recommend, unless you need some of the special features of the ListView
, as it will be more performant. 这是我推荐的方式,除非你需要
ListView
一些特殊功能,因为它会更高效。
This uses the AgentOctal.WpfLib NuGet package (I am the author of this package) for the base ViewModel
class to provide the property change notifications and Command support. 这使用AgentOctal.WpfLib NuGet包(我是此包的作者)为基本
ViewModel
类提供属性更改通知和命令支持。 You should be able to substitute any working method that properly implements INotifyPropertyChanged
though. 您应该能够替换正确实现
INotifyPropertyChanged
任何工作方法。
XAML: XAML:
<Window
x:Class="ColumnsTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ListViewColumns"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Window.DataContext>
<local:MainWindowVm />
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ListBox
Grid.Row="0"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
ItemsSource="{Binding Path=People}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Border
Height="{Binding RelativeSource={RelativeSource Self}, Path=ActualWidth}"
Margin="4"
BorderBrush="Black"
BorderThickness="1">
<TextBlock Text="{Binding Path=Name}" />
</Border>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button
Grid.Row="1"
Command="{Binding Path=AddPersonCommand}"
Content="Add Person" />
</Grid>
</Window>
View models: 查看型号:
namespace ColumnsTest
{
using System.Windows.Input;
using AgentOctal.WpfLib;
using AgentOctal.WpfLib.Commands;
class MainWindowVm : ViewModel
{
public MainWindowVm()
{
People = new ObservableCollection<PersonVm>();
}
public ObservableCollection<PersonVm> People { get; }
private ICommand _addPersonCommand;
public ICommand AddPersonCommand
{
get
{
return _addPersonCommand ?? (_addPersonCommand = new SimpleCommand((obj) =>
{
People.Add(new PersonVm() { Name = Guid.NewGuid().ToString() });
}));
}
}
class PersonVm : ViewModel
{
private string _name;
public string Name
{
get { return _name; }
set { SetValue(ref _name, value); }
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.