none
Combobox inside the Datagrid

    Question

  • Hi,

              Please Some one provide me the example for using Combobox inside the Datagrid.I tried lot but couldn't Complete it.,

    I tried using Combo inside the Dataform its working fine. I am using silverlight 4.

     

    Thanks in Advance

    Selvakumar Rathinam

    Friday, March 25, 2011 8:14 AM

All replies

  • Hi,

    Another common scenario you will often encounter when displaying a collection of items in a view is when you need the UI for each item in the collection to be associated with a command at the parent view level (instead of the item level).

    For example, in the application shown in the following illustration, the view displays a collection of items in a ListBox control, and the data template used to display each item defines a Delete button that allows the user to delete individual items from the collection.

     

    Because the view model implements the Delete command, the challenge is to wire up the Delete button in the UI for each item, to the Delete command implemented by the view model. The difficulty arises because the data context for each of the items in the ListBox references the item in the collection instead of the parent view model that implements the Delete command.

    One approach to this problem is to bind the button in the data template to the command in the parent view using the ElementName binding property to ensure that the binding is relative to the parent control and not relative to the data template. The following XAML illustrates this technique.

    <Grid x:Name="root">
        <ListBox ItemsSource="{Binding Path=Items}">
            <ListBox.ItemTemplate>
                <DataTemplate>
      <Button Content="{Binding Path=Name}"          Command="{Binding ElementName=root, Path=DataContext.DeleteCommand}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

    The content of button control in the data template is bound to the Name property on the item in the collection. However, the command for the button is bound via the root element's data context to the Delete command. This allows the button to be bound to the command at the parent view level instead of at the item level. You can use the CommandParameter property to specify the item to which the command is to be applied, or you can implement the command to operate on the currently selected item (via a CollectionView).

    Friday, March 25, 2011 8:25 AM
  • you please answer my question thats fine.

     

    Thanks

    Selva

    Friday, March 25, 2011 1:43 PM
  • Perhaps the best way to get working combobox inside Datagrid is the following:

    1. Create handler on Grid_AutoGeneratingColumn

    2. Prepare DataTemplates (Cell and CellEditing, actually only one template for Cell is enough)

    3. Then assign these DataTemplates to grid like this:

    DataGridTemplateColumn templateCol = new DataGridTemplateColumn();
              templateCol.Header = e.Column.Header;
              templateCol.CellTemplate = (DataTemplate)XamlReader.Load(s);
              //templateCol.CellEditingTemplate =
              e.Column = templateCol;
    


     

    Friday, March 25, 2011 3:47 PM
  • Hi again.

    I guess the combobox did not populate collection items.

    Could you show us the code? so that we can correct the code.

    Saturday, March 26, 2011 6:46 AM