Wpf data grid custom column sorting - wpf

How do I add a special sorting method to a particular column to allow different types of sorting(such as sorting 120.5.1.50 in between 120.5.1.12 and 120.5.1.110 instead of having 120.5.1.110 be the lowest value.
Also how do I allow click header sorting of a custom type bound it a template column. Is this even possible?

You can implement IComparer and define your own comparing logic.
public class MyComparer : IComparer<Object>
{
public int Compare(Object stringA, Object stringB)
{
// Your logic here
}
}
After you can just use LINQ OrderBy method with your custom comparer.
items = items.OrderBy(x => property, comparer).ToList();
Refer to this link.
Edit
TO override the default sorting behaviour of a WPF Datagrid, refer to the answer in this link.

If you want to maintain the custom sort order after clicking the column header, you can use an attached behaviour. I came up with this solution which seems to work well:
WPF DataGrid CustomSort for each Column
This is an MVVM solution - there are probably simpler ways of doing this if you want to delve into the world of code-behind.

Related

DataGridView with DataSource inheritance

I'm trying to create an extended version of the WinForms DataGridView (ElementDataGrid) to allow sorting and filtering. Since this will be a widget used by multiple developers, I want to hide the SortableBindingList class internally and have the user pass in just a normal List with the control creating the SortableBindingList.
I created a base class called Element, which other developers can extend, but when I set the DataPropertyName of a column to a property that isn't in Element, nothing shows up in that column. As an example, I have a Comment class that inherits from Element. I want to display the Comment Date and Comment Text in the datagrid. Neither of those columns have any data in them, but the columns using properties inherited from Element display properly.
Is there a straightforward way to have the grid display property values from classes that inherit from the Element base class? Alternately, is there a way I could have the property take a generic List?
UPDATE: Here's the method I'm using to set the data source to my SortableBindingList. As I said, properties from Element are being populated in the grid when I want to show them, but properties from Comment, which inherits from Element are not.
public List<Element> DataElements
{
set
{
bindingDataSource.Clear();
SortableBindingList<Element> boundDataSource = new SortableBindingList<Element>();
bindingDataSource.DataSource = boundDataSource;
foreach (Element e in value)
{
bindingDataSource.Add(e);
}
this.DataSource = bindingDataSource;
}
}
Take a look at Marc Gravell's answer to this SO question. Assuming, as he talks about in his answer, that the data is homogeneous (meaning that you aren't mixing Comments and SomeOtherClass in your List of Element) and has at least a single element in it (so that it is able to infer the actual type of the data in the list), I think it would work for your situation.

Retrieving Cells within a WPF DataGrid Column

I need to get all the cells within a certain column.
there is no property called "Cells" in the GridViewColumn class.
Is there any other way of doing this?
Thanks.
May I ask you the purpose of getting those cells? And when you say you want a cell, do you want the visual of that cell or the data associated with it?
If it is data, then getting it directly from your ViewModel will be a preferred approach. So if you are following proper MVVM while implementing this, you can have a method in the ViewModel which can easily expose you a specific collection of cell-data corresponds to a column
Have a look at http://techiethings.blogspot.com/2010/05/get-wpf-datagrid-row-and-cell.html

Can I use different datatemplate for same datatype basedon some creteria?

I'm new to wpf and now I have a problem. I have a model class say Customer and I've created a DataTemplate with TargetType property set to Customer. It works good. But I actually want two different templates like one for just displaying the record and another for in-place editing. Is it possible to specify two different templates for same datatype based on some creteria?
And I want to switch this template based on some property on ViewModel like when IsEditmode is True.
Or am I doing it wrong? Should I use styles instead?
Your approach seems to be perfectly fine.
You can create a DataTemplateSelector which will allow you to choose a data template based on arbitrary criteria from code behind.
I often use these to decide which template to use based on a enum-type property.
There are two easy ways I can think of, ofcourse there are other ways based on the complexity and architecture you want to follow.
Define DataTemplate with 'Key' and specifically call that either using StaticResource/DynamicResource Binding.
You can have a DataTrigger inside the datatemplate which makes some parts of the template visible/collapsed based on your 'EditMode' property

Silverlight: binding an observable collection to different usercontrols conditionally

I'm new to Silverlight 4 and having a tough time googling this one since I'm not terribly familiar with the terminology involved. Can someone point me in the right direction?
I have an observable collection that I want to represent in an ItemsControl list. The type of the collection is a class that- for simplicity's sake- let's call PersonInfo. Each PersonInfo has a string property Name and an observable collection PhoneNumbers. Everything works fine when I tell the ItemsControl's DataTemplate to use "UserControl1" for visualizing the data- the bindings work.
My problem is that- for this theoretical example- I want to base the control used to display the PersonInfo on certain values in the Name propery. So I want to use UserControl1 for any entries named "Joe", and use UserControl2 for all others. I've found IValueConverter stuff, but that doesn't seem to help with selecting the control type used to visualize the data.
Sidenote: UserControl1 and UserControl2 show data in a similar way, but there are some differences within their complicated grid layouts that forced me to create 2 separate usercontrols. If anyone knows how I could build multiple layouts into UserControl1's xaml and then switch between them at runtime via a property binding, that would probably let me sidestep this issue altogether...
Can anyone recommend a general strategy for solving either side of this problem?
Thanks in advance!
I’ve solved a similar dilemma by using an IValueConverter in an unusual way: I created a "VisibilityConverter" that tells a control whether it ought to appear. In the case of your example you would have two of them: one Convert method would
return ((PersonInfo)o).Name == "Joe" ? Visibility.Visible : Visibility.Collapsed;
and the other would do the opposite. Then bind UserControl1's visibility to one VisibilityConverter and bind UserControl2's visibility to the other and violà, they swap out based on the data.

Custom sorting logic for groups using CollectionViewSource

I have a list of "stuff" which needs to be filtered then displayed in a grouped and sorted manner. The data needed to calculate the grouping and sorting order is not available as simple properties - there needs to be some work done in code to calculate the order and groups.
CollectionViewSource lets me define custom filter and sort logic - so far so good. It also lets me bind GroupDescriptions to a value converter so that I can generate the group names.
The last thing I want to do is control the order that the generated groups appear and this is causing me pain!
Everything I see about CollectionViewSource.SortDescriptions says that it will sort groups by a property name, but I don't have a property available to sort by. SortDescriptions can't be bound to a value converter like GroupDescriptions can and I'm out of other ideas.
So - how do you implement custom sorting logic of CollectionViewSource groups?
This post on Bea Stollnitz' blog, and the GitHub repo, demonstrates how you can do that. You will have to sort by the criteria of your grouping first. Even if this is not a concrete property, it should be possible to sort your items using the same logic that you use to group them, isn't it?! Certainly, this is not possible using a SortDescription instance, but you could instead use the ListCollectionView.CustomSort property and specify an appropriate IComparer implementation.

Resources