Tôi đang gặp sự cố với WPF DataGrid
sắp xếp lại các hàng của mình khi tôi không muốn.DataGrid sắp xếp lại không mong muốn
bước:
- Áp dụng nhóm
- Sắp xếp
DataGrid
bởi một cột với bản sao trong nhóm - Thay đổi một tế bào không trong cột được sắp xếp
- Quan sát rằng các hàng sắp xếp lại di chuyển thay đổi nội dung hàng ở cuối tập hợp các hàng có giá trị khớp trong cột được sắp xếp của chúng
Ví dụ (Lưu ý sắp xếp bởi LastName):
FirstName | LastName^| Amount
----------+------------+---------
Group:Flinstone
Fred | Flinstone | 42
Wilma | Flinstone | 22
Pebbles | Flinstone | 28
Dino | Flinstone | 64
Thay đổi số lượng Wilma của 22-18 sẽ cho kết quả trong DataGrid sắp xếp lại như sau:
FirstName | LastName^| Amount
----------+------------+---------
Group:Flinstone
Fred | Flinstone | 42
Pebbles | Flinstone | 28
Dino | Flinstone | 64
Wilma | Flinstone | 18
Làm thế nào để ngăn chặn sự DataGrid
từ việc sắp xếp lại các hàng như thế này?
Lưu ý: Sắp xếp lại không mong muốn không xảy ra nếu DataGrid
không có bất kỳ nhóm nào được áp dụng cho nó.
Đây là mã tôi đang sử dụng:
ViewModel:
public class MainViewModel
{
public ObservableCollection<Item> Items { get; set; }
public ListCollectionView ItemsView { get; set; }
public MainViewModel()
{
Items = new ObservableCollection<Item>();
Items.Add(new Item("Fred", "Flinstone", 42));
Items.Add(new Item("Wilma", "Flinstone", 22));
Items.Add(new Item("Pebbles", "Flinstone", 52));
Items.Add(new Item("Dino", "Flinstone", 52));
Items.Add(new Item("Barney", "Rubble", 32));
Items.Add(new Item("Betty", "Rubble", 62));
Items.Add(new Item("BamBam", "Rubble", 42));
Items.Add(new Item("George", "Jetson", 22));
Items.Add(new Item("Jane", "Jetson", 52));
Items.Add(new Item("Judy", "Jetson", 32));
Items.Add(new Item("Elroy", "Jetson", 62));
Items.Add(new Item("Astro", "Jetson", 42));
ItemsView = new ListCollectionView(Items);
ItemsView.GroupDescriptions.Add(new PropertyGroupDescription("LastName"));
}
}
Xem:
<DataGrid
ItemsSource="{Binding ItemsView}">
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<StackPanel>
<TextBlock Text="{Binding Path=Name}" />
<ItemsPresenter />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
mẫu:
public class Item
{
public string FirstName { get; set; }
public string LastName { get; set; }
public decimal Amount { get; set; }
public Item(string firstName, string lastName, decimal amount)
{
FirstName = firstName;
LastName = lastName;
Amount = amount;
}
}
bạn có thể hiển thị mã nơi bạn thực hiện thay đổi (số tiền Wilma của 22-18)? – DrDev
@DrDev Tôi thay đổi số tiền của Wilma thông qua giao diện người dùng, không phải trong mã. –