支持分类显示,支持复选,支持下拉框选择,其中下拉框不知道为什么 DataGridComboBoxColumn 没有效果
下面例子是通过修改WPF UI第三方库中的DEMO,这个第三方库挺有意思的,感兴趣的可以研究研究,我放一个链接
WPF UI
点击查看数据类代码
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; } = "";
}
public class DataGroup
{
public DataGroup(bool selected, string name, string groupName)
{
Selected = selected;
Name = name;
GroupName = groupName;
}
public bool Selected { get; set; }
public string Name { get; set; }
public string GroupName { get; set; }
public Category Cate { get; set; } = new Category { CategoryID = 1, CategoryName = "Electronics" };
public Category[] Categories { get; set; } = [
new Category { CategoryID = 1, CategoryName = "Electronics" },
new Category { CategoryID = 2, CategoryName = "Books" },
new Category { CategoryID = 3, CategoryName = "Clothing" },
new Category { CategoryID = 4, CategoryName = "7788" },
new Category { CategoryID = 5, CategoryName = "3344" }];
//public string Category { get; set; } = "Electronics";
//public string[] Categories { get; set; } = ["Electronics", "Books", "Clothing", "7788", "3344"];
}
点击查看界面C#代码
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Data;
using Wpf.Ui.Demo.SetResources.Simple.Models;
namespace Wpf.Ui.Demo.SetResources.Simple.Views.Pages;
public partial class ExpanderPage : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
public ObservableCollection<DataGroup> GroupCollection { get; set; } = [
new DataGroup(false, "Audi", "Auto"),
new DataGroup(false, "Samsung S24 Ultra", "Phone"),
new DataGroup(true, "Apple iPhone 16 Pro", "Phone"),
new DataGroup(true, "Bugatti", "Auto"),
new DataGroup(false, "Lamborghini", "Auto")];
public ExpanderPage()
{
App.ApplyTheme(this);
InitializeComponent();
this.DataContext = this;
//this.Group.ItemsSource = GroupCollection;
ICollectionView collectionView = CollectionViewSource.GetDefaultView(GroupCollection);
collectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(DataGroup.GroupName)));
Group.ItemsSource = collectionView;
}
}
<Grid>
<ui:DataGrid x:Name="Group"
SelectionMode="Single"
SelectionUnit="Cell"
ItemsSource="{Binding}"
AutoGenerateColumns="False">
<ui:DataGrid.Columns>
<DataGridCheckBoxColumn Header="#" Binding="{Binding Selected}" />
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Header="Version" Binding="{Binding Name}" />
<DataGridTemplateColumn Header="Category" CanUserReorder="False" CanUserSort="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox SelectedItem="{Binding Category}"
DisplayMemberPath="CategoryName" SelectedValuePath="CategoryID"
ItemsSource="{Binding Categories}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</ui:DataGrid.Columns>
<ui:DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Grid>
<Expander IsExpanded="True">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock
Margin="10"
FontWeight="Bold"
Text="{Binding Name,
StringFormat=Group name: {0}}">
</TextBlock>
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ui:DataGrid.GroupStyle>
</ui:DataGrid>
</Grid>
`
浙公网安备 33010602011771号