Home Forums WPF controls Xceed DataGrid for WPF Sorting Datagrid Column which has no corresponding property on the base object

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • guidedbycthulhu
    Participant
    Post count: 4
    #44741 |

    Afternoon,

    Here is my scenario:

    I have a lit of objects, composed somewhat like:

    
    public class MyObject
    {
      string ValueA
      string ValueB
      int ValueC
      ...
      etc
    }
    
    List<MyObject> MyList;

    I have created my Datagrid, and defined the columns like so:

    <xcdg:DataGridControl ItemsSource={Binding Source={StaticResource SortableViewSource}}>
      <xcdg:DataGridControl.Columns>
        <xcdg:Column FieldName="ValueA" Title"A Value"/>
        <xcdg:Column FieldName="ValueB" Title"B Value"/>
        ...
      </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl>

    Now, I wish to apply custom sorting upon these columns. To do so, I have created a DataGridCollectionViewSource, like so:

    
    <xcdg:DataGridCollectionViewSource x:Key="SortableViewSource" Source="{Binding MyList}" AutoCreateColumns="False">
      <xcdg:DataGridCollectionViewSource.ItemProperties>
        <xcdg:DataGridItemProperty Name="ValueA" SortComparer="{StaticResource MyCustomComparer}"/>  
        <xcdg:DataGridItemProperty Name="ValueB" SortComparer="{StaticResource MyCustomComparer}"/>   
    ...
      </xcdg:DataGridCollectionViewSource.ItemProperties>
    </xcdg:DataGridCollectionViewSource>

    Everything works as expected. However, I have now added a NEW column to my datagrid. It is not bound to a property of the underlying object, instead it is multiple values run through a MultiBinding Converter.

    For example, this new column looks similar to this:

    <xcg:Column FieldName="MultiValue" Title"EdgeCase">
      <xcdg:Column.DisplayMemberBinding>
        <MultiBinding Converter="{StaticResource ResourceKey='MyMultiBindingConverter'}">
          <Binding ... />
          <Binding ... />
    ...
        </MultiBinding>
      </xcdg:Column.DisplayMemberBinding>
    </xcdg:Column>

    It also works correctly. My new column appears in the DG, displaying the values I expect.

    Now, I wish to SORT this column… Which I apparently cannot do.

    I have added a DataGridItemProperty, and discovered this one demands that I complete the DataType property. As a result, this sort of DataGridItemProperty has been added to the DataGridCollectionViewSource.ItemProperties:

    <xcdg:DataGridItemProperty Name=”MultiValue” SortComparer=”{StaticResource MyCustomComparer}” DataType=”{x:Type sys:String}”/>

    Everything compiles, and the application launches without issue. However, when I go to sort the new column, I discover that the SortComparer is getting null for both values. All the other columns, which are wired to existing properties of the underlying object sort.

    How can I sort this column, which is not wired to an existing property of the underlying object, but which is instead a value generated from a multibinding?

    I have tried different values for the DataType, including:
    (x:Type sys:Object}
    (x:Type Multibinding}
    (x:Type DataObject}

    No matter what I list as the datatype, the the SortComparer still only ever receives null values when sorting that column.

    Fawzi [Xceed]
    Member
    Post count: 722

    Hi,

    The first thing I noticed by checking your code is that you are using an ItemProperty to add your unbound column to the DataGrid. In a scenario like this, you should use the UnboundItemProperty instead and see if sort works. Could you give this a try and tell me if that fixes the issue. If not, could you send me a simple stand alone sample project which reproduces the issue so that I could have a closer look. You may send it to: support@xceed.com

    guidedbycthulhu
    Participant
    Post count: 4

    Afternoon Fawzi,

    I have changed the DataGridItemProperty to DataGridUnboundItemProperty; it still supplies nulls to the SortComparer.

    I will attempt to make an example solution as soon as possible.

    If anyone has any suggestions in the meantime, please feel free to post…

    guidedbycthulhu
    Participant
    Post count: 4

    I have created a sample project replicating the issue, and have sent it to the email you provided. I look forward to your input.

    guidedbycthulhu
    Participant
    Post count: 4

    Fawzi came up with a solution to the issue. For posterity, I am going to post the solution in case someone else wants to know the solution.

    First, remove the Multibinding completely. It should now look something like this:
    <xcg:Column FieldName=”MultiValue” Title”EdgeCase” ReadOnly=”True”/>

    Now, in the DataCollectionViewSource, you will change the DataGridUnboundProperty to resemble this:
    <xcdg:DataGridItemProperty Name=”MultiValue” SortComparer=”{StaticResource MyCustomComparer}” QueryValue=”CodeBehind_QueryValue”/>

    The important part is the QueryValue, which will be a method in our codebehind. That method will look like the following:

    private void CodeBhind_QueryValue(object sender, Xceed.wpf.datagrid.DatagridItemPropertyQueryValueEventArgs e)
    {
    e.Value = “Your custom value”;
    }

    Bam, you can now sort on the Unbounded item with the custom SortComparer.

    Thanks Fawzi!

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.