Tuesday, March 9, 2010

Infragistics - Working with 2 XamComboEditors in XamDataGrid


Recently I had to work on a sample where I had to populate the combo box values on a datagrid column field based on the selection from the first column values. I had 2 XamComboEditors in a XamDataGrid on the same row. When the first XamComboEditor changes its value, the second XamComboEditor should refresh its item source to repopulate the values based on the first comboEditor selection.
I worked on a sample application with Country/ State scenario, where the states options are based on the country selection.
XAML code
<igdp:UnboundField Name="Country" BindingPath="Country.Name" Label="Country" Column="0" IsScrollTipField="True">
    <igdp:UnboundField.Settings>
        <igdp:FieldSettings EditorType="{x:Type igEditors:XamComboEditor}">
            <igdp:FieldSettings.EditorStyle>
                <Style TargetType="{x:Type igEditors:XamComboEditor}">
                    <Setter Property="DisplayMemberPath" Value="Name" />
                    <Setter Property="ValuePath" Value="Id" />
                    <Setter Property="ItemsSource" Value="{Binding Path=DataContext.CountryCollection,
                        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
                    <Setter Property="ValueType" Value="{x:Type entities:Country}" />
                    <Setter Property="IsAlwaysInEditMode" Value="False" />
                    <Setter Property="IsEditable" Value="False" />
                    <Setter Property="DropDownButtonDisplayMode" Value="OnlyInEditMode" />
                    <EventSetter Event="SelectedItemChanged" Handler="OnCountrySelectionChanged" />
                Style>
            igdp:FieldSettings.EditorStyle>
        igdp:FieldSettings>
    igdp:UnboundField.Settings>
igdp:UnboundField>
<igdp:UnboundField Name="State" BindingPath="State.Name" Label="State" Column="1">
    <igdp:UnboundField.Settings>
        <igdp:FieldSettings EditorType="{x:Type igEditors:XamComboEditor}">
            <igdp:FieldSettings.EditorStyle>
                <Style TargetType="{x:Type igEditors:XamComboEditor}">
                    <Setter Property="DisplayMemberPath" Value="Name" />
                    <Setter Property="ValuePath" Value="Id" />
                    <Setter Property="ValueType" Value="{x:Type entities:State}" />
                    <Setter Property="IsAlwaysInEditMode" Value="False" />
                    <Setter Property="IsEditable" Value="False" />
                    <Setter Property="DropDownButtonDisplayMode" Value="OnlyInEditMode" />
                Style>
            igdp:FieldSettings.EditorStyle>
        igdp:FieldSettings>
    igdp:UnboundField.Settings>
igdp:UnboundField>
Code behind file
void OnCountrySelectionChanged(object sender, RoutedEventArgs e)
{
    DataRecord dataRecord = (DataRecord)grdSample.ActiveRecord;
    CellValuePresenter countryCellValuePresenter = CellValuePresenter.FromCell(dataRecord.Cells[0]);
    XamComboEditor countryComboEditor = countryCellValuePresenter.Editor as XamComboEditor;
    if (countryComboEditor == null) return;
    Country country = countryComboEditor.SelectedItem as Country;
   
    CellValuePresenter stateCellValuePresenter = CellValuePresenter.FromCell(dataRecord.Cells[1]);
    XamComboEditor stateComboEditor = stateCellValuePresenter.Editor as XamComboEditor;
    if (stateComboEditor == null) return;
    var states = Factory.GetStateByCountry(country.Name);
    stateComboEditor.ItemsSource = states;
    stateComboEditor.SelectedItem = states[0];
}

3 comments:

Prasanna Sarvepalli said...

Do you know how to do it pure XAML ? Also how to set the second combo with a default value (say the first item in its collection

Prajeesh Prathap said...

Hi Vasu,
I haven't tried to repopulate/ filter the second combo based on the selection of the first one. You can try to use a trigger to do that.

To setup a default value for the second combo, you can use the property binding to the selected item after the list is populated.

Kam said...

Thanks Man , You Saved my life .