Home Forums WPF controls Xceed DataGrid for WPF Accessing data item from column datatemplate

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #23131 |

    Hi group, is there a way of accessing the whole data item from a column data template rather than being limited to the field being represented by that column? Here’ s a fake scenario to make things clearer:

    1) I have a CLR class representing a Guy, with some public properties like Name, IsFemale, Job. I want to link a grid to an ObservableCollection of such Guy’s, mapping some of these properties to columns. So in XAML I declare a DataGridControl whose ItemsSource is a collection of Guy’s, and declare the columns for the properties I want to show, say Name and Job.

    2) Job is just a string; in editing it I’d like to let the user either type freely or select from a list. This list will be placed in an editable combobox in the edit template of the cell, and the combobox text property will be bound to the Job property. I want to fill the combobox items dynamically, according to the sex of the guy: so if the current data item represents a male guy a list will be programmatically provided, if she’s a female a different list will be programmatically provided (of course this is a fake scenario, but it simplifies a real-world scenario where I need to retrieve dynamically the combobox items according to some other properties of the data row item object).

    So the question is: is there a way of accessing the Guy object from the data template of a column, which by definition is limited to a single property of that object?

    Sample XAML for the Job column:

    <xcdg:Column FieldName=”Job” Title=”job”>
    <xcdg:Column.CellEditor>
    <xcdg:CellEditor>
    <xcdg:CellEditor.EditTemplate>
    <DataTemplate>
    <ComboBox Text=”{xcdg:CellEditorBinding}”
    IsEditable=”True” IsReadOnly=”False”
    Loaded=”OnJobComboLoaded”/>
    </DataTemplate>
    </xcdg:CellEditor.EditTemplate>
    </xcdg:CellEditor>
    </xcdg:Column.CellEditor>
    </xcdg:Column>

    In code-behind OnJobComboLoaded I’d need to fill the combobox items by accessing the Guy object to see whether this guy is male or female. Is there any way of doing this?
    Thanx to all!

    Imported from legacy forums. Posted by Mathetes (had 2000 views)

    Xceed Support
    Member
    Post count: 5658

    In your context… the answer is a bit tricky… The easy answer would be to modify the Column’s display member binding so that the whole object is the Column’s Data Object… However, this has a major flaw… the Cell’s edition scheme will not support your scenario very well..

    What I can propose is a little “dirty trick” that will give you access to whatever information you need from the data object…

    <DataTemplate>
    <ComboBox Tag=”{Binding RelativeSource={RelativeSource AncestorType={x:Type xcdg:DataCell}}, Path=DataContext.WhateverPropertyINeedToDoMyJob}”
    Text=”{xcdg:CellEditorBinding}”
    IsEditable=”True” IsReadOnly=”False”
    Loaded=”OnJobComboLoaded”/>
    </DataTemplate>

    Notice the Binding affected on the Tag property of the ComboBox… This nasty trick “should” do the job… (I say should because I did not try this particular scenario myself)… In the OnJobComboLoaded, you can then retrieve the tag… and play with it.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 3134 views)

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