2010-10-24 31 views
5

Trong điều khiển DataGrid của WPF, nếu bạn đặt cột cho một trong các loại cột mặc định (như DataGridTextColumn hoặc DataGridCheckBoxColumn), sắp xếp trên cột đó và sau đó thay đổi giá trị của nó, lưới sẽ tự động được sắp xếp lại.WPF DataGrid: Tự động sắp xếp lại trên DataGridTemplateColumn

Tuy nhiên, nếu bạn sử dụng DataGridTemplateColumn (và cho phép cột được sắp xếp), nó có thể được sắp xếp, nhưng thay đổi giá trị của ô trong cột này không làm cho lưới không được sắp xếp lại. Làm cách nào để tôi có thể tự động kích hoạt tính năng tự động kích hoạt sắp xếp lại?

XAML:

<DataGrid Name="grid" AutoGenerateColumns="False"> 
    <DataGrid.Columns> 
    <DataGridTextColumn Header="First name" Binding="{Binding First}"/> 
    <DataGridTemplateColumn Header="Last name" SortMemberPath="Last"> 
     <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <TextBox Text="{Binding Last}"/> 
     </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid>

Binding:

ObservableCollection items = new ObservableCollection(); 
grid.ItemsSource = items; 
items.Add(new Character() { First = "Homer", Last = "Simpson" }); 
items.Add(new Character() { First = "Kent", Last = "Brockman" }); 
items.Add(new Character() { First = "Montgomery", Last = "Burns" });

Đây là lớp mục của tôi, chỉ trong trường hợp có liên quan:

public class Character : INotifyPropertyChanged { 
    private string first, last; 
    public event PropertyChangedEventHandler PropertyChanged; 
    private void Notify(string name) { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(name)); 
    } 
    public string First { get { return first; } set { first = value; Notify("First"); } } 
    public string Last { get { return last; } set { last = value; Notify("Last"); } } 
}
+0

vẻ khi được làm việc tốt cho tôi. Bạn có nghĩa là bạn thay đổi giá trị SortMemberPath cho DataGridTemplateColumn? –

+1

Nó sắp xếp khi bạn bấm vào nó một cách rõ ràng, hoặc khi bạn thay đổi giá trị của cột FirstName, nhưng không sắp xếp lại khi bạn thay đổi giá trị cột LastName. Đặt SortMemberPath cho phép nó sắp xếp, nhưng không làm cho nó * tự động * sắp xếp lại, do đó vấn đề. –

+0

Trong tình huống này, tôi quan sát những thay đổi của một tài sản cần thiết và được gọi là SomeCollectionViewOfGrid.Refresh(); – vorrtex

Trả lời

4

Tôi cũng đang tìm câu trả lời cho điều này . Tôi tìm thấy một giải pháp: (không hài lòng với nó, nhưng ...)

Khi bộ sưu tập của bạn được cập nhật, bạn có thể làm điều này:

SortDescription sortDescription = grdData.Items.SortDescriptions[0]; 
grdData.ItemsSource = null; 
grdData.ItemsSource = Data; 
grdData.Items.SortDescriptions.Add(sortDescription); 

Ugly nhưng nó làm việc. Bạn sẽ muốn lưu trữ toàn bộ bộ sưu tập không giống như ví dụ của tôi mà chỉ mục đầu tiên.

Một vấn đề với nó mặc dù là DataGrid mất tiêu đề cho biết sắp xếp, do đó, mặc dù nó khu nghỉ dưỡng chính xác tiêu đề cột không còn được chọn với mũi tên hiển thị hướng sắp xếp.

+0

Không cần phải đặt lại 'ItemsSource'. Trong ứng dụng của tôi 'SortDescriptions.Clear();' thay vào đó hoạt động hoàn hảo. –

0

Tôi đã có DataGrid trong C# WPF theo VS2010 sẽ không sắp xếp bất kể cài đặt XAML. Đối với một số lý do này DataGrid ẩn (trên một tab thứ cấp) đã có vấn đề với thứ tự sắp xếp nơi DataGrid chính là tốt với các thiết lập tương tự. Như vậy tôi đã phải sơ đồ sắp xếp DataGrid. Dưới đây là ghi chú của tôi:

Đầu tiên là XAML cho hai lưới dữ liệu sủ (tiểu học và trung học, chúng tôi sẽ chỉ được sắp xếp thứ hai "tên mở rộng" lưới:

 <TabControl Grid.Row="1" Name="tabControl1" VerticalAlignment="Top" Style="{StaticResource Section}" Margin="3" Padding="0" FontFamily="Arial" FontSize="10" BorderThickness="0" > 
      <TabItem Name="tabCommon" Style="{StaticResource NameTab}"> 
       <DataGrid Name="grdCommonNames" SelectionChanged="grdCommonNames_SelectionChanged" PreviewKeyDown="grdCommonNames_PreviewKeyDown" Style="{StaticResource NameListGrid}" Focusable="False"> 
        <DataGrid.Columns> 
         <DataGridTextColumn Binding="{Binding Name, NotifyOnTargetUpdated=True}" Width="SizeToCells" Header="Name" CellStyle="{StaticResource NameListCol}" SortDirection="Ascending" /> 
         <DataGridTextColumn Binding="{Binding Pronunciation, NotifyOnTargetUpdated=True}" Width="SizeToCells" Header="Pronunciation" CellStyle="{StaticResource NameListRightCol}"/> 
        </DataGrid.Columns> 
       </DataGrid> 
      </TabItem> 
      <TabItem Name="tabExtended" Style="{StaticResource NameTab}"> 
       <DataGrid Name="grdExtendedNames" SelectionChanged="grdCommonNames_SelectionChanged" PreviewKeyDown="grdCommonNames_PreviewKeyDown" Style="{StaticResource NameListGrid}" > 
        <DataGrid.Columns> 
         <DataGridTextColumn Binding="{Binding Name, NotifyOnTargetUpdated=True}" Width="SizeToCells" Header="Name" CellStyle="{StaticResource NameListCol}" SortDirection="Descending" SortMemberPath="Name"/> 
         <DataGridTextColumn Binding="{Binding Pronunciation, NotifyOnTargetUpdated=True}" Width="SizeToCells" Header="Pronunciation" CellStyle="{StaticResource NameListRightCol}"/> 
        </DataGrid.Columns> 
       </DataGrid> 
      </TabItem> 
     </TabControl> 

Sau đó, đoạn mã để sắp xếp thứ hai Chúng ta chỉ phân loại lần đầu tiên, đó là lý do tại sao boolean lại ở đây, theo cách đó nếu chúng sắp xếp theo các cột khác theo cách thủ công, nó sẽ được giữ lại ngay cả khi chúng quay trở lại tab đầu tiên và sau đó truy cập lại vào tab thứ 2.

Tại đây cột đầu tiên của chúng tôi trong Datagrid được đặt tên là "Tên". Đoạn mã trên nhấp chuột:

 if (!extendSorted) 
     { 
      SortDescription extSort = new SortDescription("Name", ListSortDirection.Ascending); 
      grdExtendedNames.Items.SortDescriptions.Add(extSort); 
      extendSorted = true; 
     } 

Hy vọng giúp người khác sắp xếp dữ liệu của họ thông qua mã. Hầu hết các ví dụ khác mà chúng tôi tìm thấy hoạt động tốt cho các thiết lập đơn giản, nhưng trong thiết lập tab kép datagrid này, nó đã phân loại ra khỏi whack.

0

Tôi gặp sự cố tương tự, khi chèn hàng mới vào DataGrid. Tôi đã giải quyết vấn đề này bằng cách làm mới các mục của DataGrid.

dataGrid.Items.Refresh(). Điều này cũng phục hồi phân loại.
Đừng quên để thiết lập SortDirection tại DataGridColumn (trong trường hợp này đó là một DataGridTextColumn) định nghĩa

DataGrid:

<DataGrid x:Name="dgCustomers" ItemsSource="{Binding CustomerTable}" AutoGenerateColumns="False" CanUserDeleteRows="True"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="Kunden ID" Binding="{Binding Path=KundenID,Mode=TwoWay}" SortDirection="Ascending" /> 
      <DataGridTextColumn Header="Name" Binding="{Binding Path=Kundenname,Mode=TwoWay}"/> 
    </DataGrid.Columns> 
</DataGrid> 

CS file:

private void btnSavecustomerChanges_Click(object sender, RoutedEventArgs e) 
{   
    try   
    { 
     BL.UpdateCustomerChanges(); 
     dgCustomers.Items.Refresh(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message, "Fehler beim Speichern", MessageBoxButton.OK, MessageBoxImage.Error); 
    } 
} 
0
If DataGridMain.Items.SortDescriptions.Count > 0 Then 
     Dim vSortDescColl As New SortDescriptionCollection 
     For Each vSortDesc In DataGridMain.Items.SortDescriptions 
      vSortDescColl.Add(vSortDesc) 
     Next 
     DataGridMain.ItemsSource = Nothing 
     DataGridMain.ItemsSource = vCallColl 
     For Each vSortDesc In vSortDescColl 
      DataGridMain.Items.SortDescriptions.Add(vSortDesc) 
      For Each vColumn In DataGridMain.Columns 
       If vColumn.SortMemberPath = vSortDesc.PropertyName Then 
        vColumn.SortDirection = vSortDesc.Direction 
        Exit For 
       End If 
      Next 
     Next 
    End If 
+0

Tôi khuyên bạn nên thêm một chút tường thuật để mở đầu mã của bạn để giải thích những gì bạn đang làm và tại sao bạn thêm câu trả lời này cho câu hỏi hai tuổi. – Rob

4

Tôi biết đây là cũ nhưng tôi cũng có vấn đề này sắp xếp lại DataGridTemplateColumn. Điều này không xảy ra trên DataGridTextColumn. Con đường tôi sửa chữa nó với hướng loại còn nguyên vẹn trên tiêu đề cột là:

// after updating the collection, remove all SortDescription and add'em back. 
SortDescriptionCollection sortDescriptions = new SortDescriptionCollection(); 
foreach (SortDescription sd in dataGrid.Items.SortDescriptions) 
{ 
    sortDescriptions.Add(sd); 
} 
dataGrid.Items.SortDescriptions.Clear(); 

foreach (SortDescription sd in sortDescriptions) 
{ 
    dataGrid.Items.SortDescriptions.Add(sd); 
} 

Hope this helps người.

0

Không ai trong số đề tài câu trả lời làm việc cho tôi trong năm 2016.

Sau một số thử & lỗi tôi đến với điều này và nó dường như chỉ làm việc tốt:

dataGrid.Items.IsLiveSorting = true; 
Các vấn đề liên quan