locked
Resetting / Clearing a ComboBox in a DataGrid RRS feed

  • Question

  • I am using a ComboBox in a DataGrid edit template. It appears that the initial instance of combobox is not destroyed upon unloading; it seems to be reused.

    It there a way to do so or at least clear the ComboBox's items list? Clear() doesn't seem to do the job. I did find a suggestion about assigning a list, then a null to the items source in order to get past the read only restriction on the items list to use Clear(); but even that doesn't clear the list. 

    Insights and suggestions appreciated.

    Marc

    Saturday, September 18, 2010 3:59 PM

Answers

  • It appears that you can clear the context of the data collection, i.e. Context.ActivitySubTypes.Clear(), and that will trigger the combobox to clear itself, then re-query the ItemsSource.

    Marc

    Tuesday, September 21, 2010 10:24 PM

All replies

  • Hi!

    I have cleared ComboBox items.

    ComboBox cboCountry = (ComboBox)DataFormDepartmentInfo.FindNameInContent("ComboBoxCountry");
    cboCountry.Items.Clear();


    Probably I have not understood your question clearly.

    Saturday, September 18, 2010 4:57 PM
  • Thanks for replying.

    I would expect that your sample would work. Here is the full scenario:

    The combobox is embedded in an CellEditTemplate in a DataGrid column and bound to WCF RIA Entities:

    <datagrid:DataGridTemplateColumn x:Name="activitySubTypeNameColumn" Header="Sub-Activity" Width="150">
      <datagrid:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <TextBlock x:Name="CT_TBL_SubActivity" 
             Height="23" 
             Margin="3" 
             TextAlignment="Right">
           <TextBlock.Text>
            <Binding Path="ActivitySubTypeName" />
           </TextBlock.Text>
          </TextBlock>
        </DataTemplate>
      </datagrid:DataGridTemplateColumn.CellTemplate>
      <datagrid:DataGridTemplateColumn.CellEditingTemplate>
      <DataTemplate>
        <ComboBox x:Name="CET_CB_SubActivity" 
           Width="140"                                                            
           Height="23" 
           Margin="3" 
           ItemsSource="{Binding Source={StaticResource LookupProvider_SubActivityTypes}, Path=Entities, Mode=OneWay, UpdateSourceTrigger=Default}"
           DisplayMemberPath="Name"
           SelectedValuePath="PK"
           SelectedValue="{Binding ActivitySubType, Mode=TwoWay}" 
           Unloaded="CET_CB_SubActivity_Unloaded">
         <ComboBoxItem/>
        </ComboBox>
      </DataTemplate>
     </datagrid:DataGridTemplateColumn.CellEditingTemplate>
    </datagrid:DataGridTemplateColumn>

    When the editing ends, and all subsequent processing is down, the following event handler is called. The list is read only, hence manipulation seen which doesn't work anyway:

    private void CET_CB_SubActivity_Unloaded(object sender, RoutedEventArgs e)
            {
                ComboBox box = sender as ComboBox;
                List<object> list = new List<object>();
                box.ItemsSource = list;
                box.ItemsSource = null;
                box.Items.Clear();
            }

    The ComboBox SubActivity selections are determined by the Activity type; so each row may have a different Activity type. The possible subactivity types of the previous rows edited are present with the new subactivity types when I edit the next row; hence how I know that the ComboBox is not being destroyed or cleared.

    Saturday, September 18, 2010 5:39 PM
  • It appears that you can clear the context of the data collection, i.e. Context.ActivitySubTypes.Clear(), and that will trigger the combobox to clear itself, then re-query the ItemsSource.

    Marc

    Tuesday, September 21, 2010 10:24 PM