2

I have a DataGrid which is binded to a collection of models. My model has a string property called Position which is of type string, but contains an integer value.

My problem is, when sorting this column values get sorted as 1, 10, 2 instead of 1, 2, 10.

Is there anyway to handle this MVVM style without handling the Sorting event of the DataGrid in code-behind? Bear in mind I cannot change the type of the property.

2
  • 1
    put an int PositionNumber {get { return int.Parse(Position); } } property in the model and use that instead. Commented Mar 6, 2014 at 19:34
  • 1
    @HighCore I have more than one property with this behavior and I'm looking for a more generic solution Commented Mar 6, 2014 at 20:01

1 Answer 1

5

If you don't want to do it in the code-behind, I think you will want a custom sort behavior, like shown in this SO post...

https://stackoverflow.com/a/18218963/1246574

I know you said you don't want to break MVVM so you don't want to handle a sort event in the code-behind. Even so, in case anyone else wants to know how to do it that way, this SO post shows how...

https://stackoverflow.com/a/2130557/1246574

Sign up to request clarification or add additional context in comments.

1 Comment

The attached behavior seems to be what I'm looking for. I'll check it out and report back. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.