WPF GROUPED Datagrid: how to sort groups themselves by vm property?












1















Scenario: a datagrid to show custom events from custom sources.
The grid must group rows by source, ordering events by datetime descending (newer always on top). No manual sorting allowed, virtualization enabled, MVVM pattern.
Here's my issue: the group header itself must follow the datetime ordering, that is the group containg the latest event must scale to top.



ViewModel:



public class Event
{
public Int32 Source_Id { get; set; }
public String Source_Name { get; set; }
public DateTime Event_DateTime { get; set; }
}


CollectionView is grouped by Source_Id and sorted by Event_DateTime:



<CollectionViewSource x:Key='alarms_src' Source="{Binding Alarms}">                
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Source_Id" />
</CollectionViewSource.GroupDescriptions>
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Event_DateTime" Direction="Descending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>


GroupStyle is a simple expander with source name and latest event datetime into the header:



<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="4"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" Background="LightGray">
<Expander.Header>

<StackPanel Orientation="Horizontal" Background="Transparent">

<TextBlock Margin="8" Text="{Binding Items[0].Source_Name}"/>
<TextBlock Margin="8" Text="{Binding Items[0].Event_DateTime}"/>

</StackPanel>
</Expander.Header>

<Expander.Content>
<ItemsPresenter/>
</Expander.Content>

</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>


Output:
simplified grid



The red arrows in the image points to the 4th row I've generated, with the latest timestamp.
The grid is correctly grouping and sorting INSIDE each group, but what I expect is that the Device_1 group would scale on top of the grid: how to obtain this?



Update: wetransfer link to sample project










share|improve this question

























  • This is a similar question with answer: How can I order Groups in WPF

    – Coops
    Nov 23 '18 at 10:34











  • GroupDescriptor has a CustomSort property. Maybe take advantage of it?

    – icebat
    Nov 23 '18 at 11:35













  • @Fabio Could you provide your GroupStyle Xaml and if you have used any converter for the Xaml paste that code too, it will help

    – Satish Pai
    Nov 23 '18 at 17:54











  • @SatishPai: requested code added

    – Fabio
    Nov 26 '18 at 13:09











  • @Fabio , Since you mentioned "but what I expect is that the Device_1 group would scale on top of the grid: how to obtain this?" i missed that you were mentioning about timestamp, will get back to you on this.

    – Satish Pai
    Nov 28 '18 at 19:18
















1















Scenario: a datagrid to show custom events from custom sources.
The grid must group rows by source, ordering events by datetime descending (newer always on top). No manual sorting allowed, virtualization enabled, MVVM pattern.
Here's my issue: the group header itself must follow the datetime ordering, that is the group containg the latest event must scale to top.



ViewModel:



public class Event
{
public Int32 Source_Id { get; set; }
public String Source_Name { get; set; }
public DateTime Event_DateTime { get; set; }
}


CollectionView is grouped by Source_Id and sorted by Event_DateTime:



<CollectionViewSource x:Key='alarms_src' Source="{Binding Alarms}">                
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Source_Id" />
</CollectionViewSource.GroupDescriptions>
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Event_DateTime" Direction="Descending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>


GroupStyle is a simple expander with source name and latest event datetime into the header:



<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="4"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" Background="LightGray">
<Expander.Header>

<StackPanel Orientation="Horizontal" Background="Transparent">

<TextBlock Margin="8" Text="{Binding Items[0].Source_Name}"/>
<TextBlock Margin="8" Text="{Binding Items[0].Event_DateTime}"/>

</StackPanel>
</Expander.Header>

<Expander.Content>
<ItemsPresenter/>
</Expander.Content>

</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>


Output:
simplified grid



The red arrows in the image points to the 4th row I've generated, with the latest timestamp.
The grid is correctly grouping and sorting INSIDE each group, but what I expect is that the Device_1 group would scale on top of the grid: how to obtain this?



Update: wetransfer link to sample project










share|improve this question

























  • This is a similar question with answer: How can I order Groups in WPF

    – Coops
    Nov 23 '18 at 10:34











  • GroupDescriptor has a CustomSort property. Maybe take advantage of it?

    – icebat
    Nov 23 '18 at 11:35













  • @Fabio Could you provide your GroupStyle Xaml and if you have used any converter for the Xaml paste that code too, it will help

    – Satish Pai
    Nov 23 '18 at 17:54











  • @SatishPai: requested code added

    – Fabio
    Nov 26 '18 at 13:09











  • @Fabio , Since you mentioned "but what I expect is that the Device_1 group would scale on top of the grid: how to obtain this?" i missed that you were mentioning about timestamp, will get back to you on this.

    – Satish Pai
    Nov 28 '18 at 19:18














1












1








1


1






Scenario: a datagrid to show custom events from custom sources.
The grid must group rows by source, ordering events by datetime descending (newer always on top). No manual sorting allowed, virtualization enabled, MVVM pattern.
Here's my issue: the group header itself must follow the datetime ordering, that is the group containg the latest event must scale to top.



ViewModel:



public class Event
{
public Int32 Source_Id { get; set; }
public String Source_Name { get; set; }
public DateTime Event_DateTime { get; set; }
}


CollectionView is grouped by Source_Id and sorted by Event_DateTime:



<CollectionViewSource x:Key='alarms_src' Source="{Binding Alarms}">                
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Source_Id" />
</CollectionViewSource.GroupDescriptions>
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Event_DateTime" Direction="Descending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>


GroupStyle is a simple expander with source name and latest event datetime into the header:



<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="4"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" Background="LightGray">
<Expander.Header>

<StackPanel Orientation="Horizontal" Background="Transparent">

<TextBlock Margin="8" Text="{Binding Items[0].Source_Name}"/>
<TextBlock Margin="8" Text="{Binding Items[0].Event_DateTime}"/>

</StackPanel>
</Expander.Header>

<Expander.Content>
<ItemsPresenter/>
</Expander.Content>

</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>


Output:
simplified grid



The red arrows in the image points to the 4th row I've generated, with the latest timestamp.
The grid is correctly grouping and sorting INSIDE each group, but what I expect is that the Device_1 group would scale on top of the grid: how to obtain this?



Update: wetransfer link to sample project










share|improve this question
















Scenario: a datagrid to show custom events from custom sources.
The grid must group rows by source, ordering events by datetime descending (newer always on top). No manual sorting allowed, virtualization enabled, MVVM pattern.
Here's my issue: the group header itself must follow the datetime ordering, that is the group containg the latest event must scale to top.



ViewModel:



public class Event
{
public Int32 Source_Id { get; set; }
public String Source_Name { get; set; }
public DateTime Event_DateTime { get; set; }
}


CollectionView is grouped by Source_Id and sorted by Event_DateTime:



<CollectionViewSource x:Key='alarms_src' Source="{Binding Alarms}">                
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Source_Id" />
</CollectionViewSource.GroupDescriptions>
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Event_DateTime" Direction="Descending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>


GroupStyle is a simple expander with source name and latest event datetime into the header:



<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="4"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" Background="LightGray">
<Expander.Header>

<StackPanel Orientation="Horizontal" Background="Transparent">

<TextBlock Margin="8" Text="{Binding Items[0].Source_Name}"/>
<TextBlock Margin="8" Text="{Binding Items[0].Event_DateTime}"/>

</StackPanel>
</Expander.Header>

<Expander.Content>
<ItemsPresenter/>
</Expander.Content>

</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>


Output:
simplified grid



The red arrows in the image points to the 4th row I've generated, with the latest timestamp.
The grid is correctly grouping and sorting INSIDE each group, but what I expect is that the Device_1 group would scale on top of the grid: how to obtain this?



Update: wetransfer link to sample project







c# wpf sorting datagrid grouping






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 12:11







Fabio

















asked Nov 23 '18 at 10:11









FabioFabio

62




62













  • This is a similar question with answer: How can I order Groups in WPF

    – Coops
    Nov 23 '18 at 10:34











  • GroupDescriptor has a CustomSort property. Maybe take advantage of it?

    – icebat
    Nov 23 '18 at 11:35













  • @Fabio Could you provide your GroupStyle Xaml and if you have used any converter for the Xaml paste that code too, it will help

    – Satish Pai
    Nov 23 '18 at 17:54











  • @SatishPai: requested code added

    – Fabio
    Nov 26 '18 at 13:09











  • @Fabio , Since you mentioned "but what I expect is that the Device_1 group would scale on top of the grid: how to obtain this?" i missed that you were mentioning about timestamp, will get back to you on this.

    – Satish Pai
    Nov 28 '18 at 19:18



















  • This is a similar question with answer: How can I order Groups in WPF

    – Coops
    Nov 23 '18 at 10:34











  • GroupDescriptor has a CustomSort property. Maybe take advantage of it?

    – icebat
    Nov 23 '18 at 11:35













  • @Fabio Could you provide your GroupStyle Xaml and if you have used any converter for the Xaml paste that code too, it will help

    – Satish Pai
    Nov 23 '18 at 17:54











  • @SatishPai: requested code added

    – Fabio
    Nov 26 '18 at 13:09











  • @Fabio , Since you mentioned "but what I expect is that the Device_1 group would scale on top of the grid: how to obtain this?" i missed that you were mentioning about timestamp, will get back to you on this.

    – Satish Pai
    Nov 28 '18 at 19:18

















This is a similar question with answer: How can I order Groups in WPF

– Coops
Nov 23 '18 at 10:34





This is a similar question with answer: How can I order Groups in WPF

– Coops
Nov 23 '18 at 10:34













GroupDescriptor has a CustomSort property. Maybe take advantage of it?

– icebat
Nov 23 '18 at 11:35







GroupDescriptor has a CustomSort property. Maybe take advantage of it?

– icebat
Nov 23 '18 at 11:35















@Fabio Could you provide your GroupStyle Xaml and if you have used any converter for the Xaml paste that code too, it will help

– Satish Pai
Nov 23 '18 at 17:54





@Fabio Could you provide your GroupStyle Xaml and if you have used any converter for the Xaml paste that code too, it will help

– Satish Pai
Nov 23 '18 at 17:54













@SatishPai: requested code added

– Fabio
Nov 26 '18 at 13:09





@SatishPai: requested code added

– Fabio
Nov 26 '18 at 13:09













@Fabio , Since you mentioned "but what I expect is that the Device_1 group would scale on top of the grid: how to obtain this?" i missed that you were mentioning about timestamp, will get back to you on this.

– Satish Pai
Nov 28 '18 at 19:18





@Fabio , Since you mentioned "but what I expect is that the Device_1 group would scale on top of the grid: how to obtain this?" i missed that you were mentioning about timestamp, will get back to you on this.

– Satish Pai
Nov 28 '18 at 19:18












1 Answer
1






active

oldest

votes


















0














CollectionViewSource.GetDefaultView(testdg.ItemsSource).Refresh(); 


Here testdg is my DataGrid Control name.



What i thought is to refresh the itemsource of the datagrid and the rest sorting will be taken care. Checkout the method.



    private void addEvent(Int32 source)
{
Event a = new Event();
a.Source_Id = source;
a.Source_Name = "Device_" + source.ToString();
a.Event_DateTime = DateTime.Now;

this.Alarms.Add(a);
CollectionViewSource.GetDefaultView(testdg.ItemsSource).Refresh();
}


Sorry, misunderstood your question previously.






share|improve this answer


























  • @Fabio Did this solution work for you ? I was waiting for long time.

    – Satish Pai
    Nov 29 '18 at 13:42













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444648%2fwpf-grouped-datagrid-how-to-sort-groups-themselves-by-vm-property%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














CollectionViewSource.GetDefaultView(testdg.ItemsSource).Refresh(); 


Here testdg is my DataGrid Control name.



What i thought is to refresh the itemsource of the datagrid and the rest sorting will be taken care. Checkout the method.



    private void addEvent(Int32 source)
{
Event a = new Event();
a.Source_Id = source;
a.Source_Name = "Device_" + source.ToString();
a.Event_DateTime = DateTime.Now;

this.Alarms.Add(a);
CollectionViewSource.GetDefaultView(testdg.ItemsSource).Refresh();
}


Sorry, misunderstood your question previously.






share|improve this answer


























  • @Fabio Did this solution work for you ? I was waiting for long time.

    – Satish Pai
    Nov 29 '18 at 13:42


















0














CollectionViewSource.GetDefaultView(testdg.ItemsSource).Refresh(); 


Here testdg is my DataGrid Control name.



What i thought is to refresh the itemsource of the datagrid and the rest sorting will be taken care. Checkout the method.



    private void addEvent(Int32 source)
{
Event a = new Event();
a.Source_Id = source;
a.Source_Name = "Device_" + source.ToString();
a.Event_DateTime = DateTime.Now;

this.Alarms.Add(a);
CollectionViewSource.GetDefaultView(testdg.ItemsSource).Refresh();
}


Sorry, misunderstood your question previously.






share|improve this answer


























  • @Fabio Did this solution work for you ? I was waiting for long time.

    – Satish Pai
    Nov 29 '18 at 13:42
















0












0








0







CollectionViewSource.GetDefaultView(testdg.ItemsSource).Refresh(); 


Here testdg is my DataGrid Control name.



What i thought is to refresh the itemsource of the datagrid and the rest sorting will be taken care. Checkout the method.



    private void addEvent(Int32 source)
{
Event a = new Event();
a.Source_Id = source;
a.Source_Name = "Device_" + source.ToString();
a.Event_DateTime = DateTime.Now;

this.Alarms.Add(a);
CollectionViewSource.GetDefaultView(testdg.ItemsSource).Refresh();
}


Sorry, misunderstood your question previously.






share|improve this answer















CollectionViewSource.GetDefaultView(testdg.ItemsSource).Refresh(); 


Here testdg is my DataGrid Control name.



What i thought is to refresh the itemsource of the datagrid and the rest sorting will be taken care. Checkout the method.



    private void addEvent(Int32 source)
{
Event a = new Event();
a.Source_Id = source;
a.Source_Name = "Device_" + source.ToString();
a.Event_DateTime = DateTime.Now;

this.Alarms.Add(a);
CollectionViewSource.GetDefaultView(testdg.ItemsSource).Refresh();
}


Sorry, misunderstood your question previously.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 29 '18 at 11:57

























answered Nov 28 '18 at 19:32









Satish PaiSatish Pai

7841510




7841510













  • @Fabio Did this solution work for you ? I was waiting for long time.

    – Satish Pai
    Nov 29 '18 at 13:42





















  • @Fabio Did this solution work for you ? I was waiting for long time.

    – Satish Pai
    Nov 29 '18 at 13:42



















@Fabio Did this solution work for you ? I was waiting for long time.

– Satish Pai
Nov 29 '18 at 13:42







@Fabio Did this solution work for you ? I was waiting for long time.

– Satish Pai
Nov 29 '18 at 13:42




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444648%2fwpf-grouped-datagrid-how-to-sort-groups-themselves-by-vm-property%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'