gpt4 book ai didi

wpf - 手动分组 DataGrid 项目

转载 作者:行者123 更新时间:2023-12-05 00:00:25 25 4
gpt4 key购买 nike

我需要使用数据网格,我的数据如下所示:
名字、姓氏、街道、 zip 、城市、国家、图像

在我的数据网格中,我只会显示名字、姓氏和图像,但它必须在城市之后分组。

更新
下面的代码显示了分组的项目,但我要显示的三个项目(firstName、lastName、image)后面是每行的所有项目(firstName、lastName、street、zip、city、country、image)。我想我必须更换<ItemsPresenter />但这只是猜测..

谁能帮帮我,我一个人无法解决这个问题...

<Grid>
<DataGrid ItemsSource="{Binding GroupedMovables}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Preview" Width="SizeToCells" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Name="Preview" Height="20" Source="{Binding Image}" HorizontalAlignment="Center" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="first name" Binding="{Binding FirstName}" />
<DataGridTextColumn Header="last name" Binding="{Binding LastName}" />
</DataGrid.Columns>
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=City}" FontWeight="Bold" Padding="3"/>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander>
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text="{Binding Path=ItemCount}" Margin="8,0,4,0"/>
<TextBlock Text="Element(s)"/>
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
</Grid>

最佳答案

分组的正确方法是使用 CollectionView (更多详情:How to Navigate, Group, Sort and Filter Data in WPF)。以下是我为您创建的一个简单的概念验证应用程序,用于向您展示如何使用 CollectionView用于对您的数据进行分组:

此类表示 DataGrid 中的一行:

public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Street { get; set; }
public string ZipCode { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Image { get; set; }
}

背后的 MaindWindow 代码:
    /// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

// Create some test data
var employees =
new ObservableCollection<Employee>
{
new Employee {FirstName = "Mohammed", LastName = "Fadil", Street = "A B C", ZipCode = "123", City = "London", Country = "UK", Image = "/Images/globe.png"},
new Employee {FirstName = "Siraj", LastName = "Hussam", Street = "A B C", ZipCode = "123", City = "London", Country = "UK", Image = "/Images/globe.png"},
new Employee {FirstName = "Ayman", LastName = "Tariq", Street = "A B C", ZipCode = "123", City = "London", Country = "UK", Image = "/Images/globe.png"},
new Employee {FirstName = "Khalid", LastName = "Sheik", Street = "X Y Z", ZipCode = "234", City = "Paris", Country = "France", Image = "/Images/monitor.png"},
new Employee {FirstName = "Hassan", LastName = "Ali", Street = "Q W E R", ZipCode = "544", City = "NY", Country = "USA", Image = "/Images/star.png"},
new Employee {FirstName = "Ehsan", LastName = "Mahmoud", Street = "A B C", ZipCode = "123", City = "London", Country = "UK", Image = "/Images/globe.png"},
new Employee {FirstName = "Idris", LastName = "Sheik", Street = "X Y Z", ZipCode = "234", City = "Paris", Country = "France", Image = "/Images/monitor.png"},
new Employee {FirstName = "Khalil", LastName = "Ali", Street = "Q W E R", ZipCode = "544", City = "NY", Country = "USA", Image = "/Images/star.png"}
};

ICollectionView employeesView =
CollectionViewSource.GetDefaultView(employees);

// Set the grouping by city proprty
employeesView.GroupDescriptions.Add(new PropertyGroupDescription("City"));

// Set the view as the DataContext for the DataGrid
EmployeesDataGrid.DataContext = employeesView;
}
}

DataGrid XAML 代码:
    <DataGrid Name="EmployeesDataGrid" ItemsSource="{Binding}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding FirstName}" Header="First Name"/>
<DataGridTextColumn Binding="{Binding LastName}" Header="Last Name"/>
<DataGridTemplateColumn Header="Image">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Image}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>

<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=City}" FontWeight="Bold" Padding="3"/>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander>
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text="{Binding Path=ItemCount}" Margin="8,0,4,0"/>
<TextBlock Text="Element(s)"/>
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>

</DataGrid>

结果是:

Result

有关设置 DataGrid 组样式的更多信息,请查看此帖子: WPF DataGrid Control > Grouping

关于wpf - 手动分组 DataGrid 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10283720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com