Skip to main content

Microsoft Silverlight

Answered Question DataBinding: using a listbox inside a DataGridRSS Feed

(0)

brauliod
brauliod

Participant

Participant

1169 points

472 Posts

Silverlight MVP

DataBinding: using a listbox inside a DataGrid

Hi,

  I'm using a DataGrid to display some binded data. In order to let the user edit online the rows of the data grid I have created several CellEditingTemplates, If I use for instance a textbox for editing a column, works fine...

   BUT... what happens if I want to use a listbox in that data template? How can I indicate the ItemsSource of that ListBox ? (the data binded only contains the main entity) Should I create my own entity that contains all the data necessary? (something like a crappy dataset?).

   Thanks in advance

     Braulio

// ---------------------------------
    Braulio Diez

    http://www.dbschemaeditor.com
    Free Silverlight Based Database Schema Editor
/// ---------------------------------

lee_sl
lee_sl

Contributor

Contributor

2992 points

585 Posts

Re: DataBinding: using a listbox inside a DataGrid

you should be able to set the ItemsSource in BeginningEdit event handler

 

----------------------------------------------
Available for consulting in Dallas, TX
http://leeontech.wordpress.com/

brauliod
brauliod

Participant

Participant

1169 points

472 Posts

Silverlight MVP

Re: DataBinding: using a listbox inside a DataGrid

Thanks for the tip but... how can I retrieve from this event the instance of the listbox that is being shown?

 

// ---------------------------------
    Braulio Diez

    http://www.dbschemaeditor.com
    Free Silverlight Based Database Schema Editor
/// ---------------------------------

lee_sl
lee_sl

Contributor

Contributor

2992 points

585 Posts

Re: Re: DataBinding: using a listbox inside a DataGrid

you can use GetCellContent() method to get the content of the cell, which you will cast to a listbox in your case

----------------------------------------------
Available for consulting in Dallas, TX
http://leeontech.wordpress.com/

brauliod
brauliod

Participant

Participant

1169 points

472 Posts

Silverlight MVP

Re: Re: Re: DataBinding: using a listbox inside a DataGrid

Thanks... quite near to the solution, but...

 e.Column.GetCellContent(e.Row)

 Returns me the control used for display mode, Is there any other event where the grid returns me the control used in edit mode / template?

 Thanks

// ---------------------------------
    Braulio Diez

    http://www.dbschemaeditor.com
    Free Silverlight Based Database Schema Editor
/// ---------------------------------

Brauliod
Brauliod

Participant

Participant

1169 points

472 Posts

Silverlight MVP

Re: Re: Re: Re: DataBinding: using a listbox inside a DataGrid

Hi Lee, 

 It seems that the event to hook is:

PreparingCellForEdit

 This event is fired after the control is loaded.

 Thanks a lot for your help, as soon as I get the full solution working I will post it here (then another dummy like me can perform the binding :-)).

 

 

// ---------------------------------
    Braulio Diez

    http://www.dbschemaeditor.com
    Free Silverlight Based Database Schema Editor
/// ---------------------------------

lee_sl
lee_sl

Contributor

Contributor

2992 points

585 Posts

Re: Re: Re: Re: DataBinding: using a listbox inside a DataGrid

you are  right about the event, PreparingCellForEdit is the right one. if we move the code you wrote before to a handler for this event, it should work

----------------------------------------------
Available for consulting in Dallas, TX
http://leeontech.wordpress.com/

brauliod
brauliod

Participant

Participant

1169 points

472 Posts

Silverlight MVP

Re: Re: Re: Re: DataBinding: using a listbox inside a DataGrid

Yes, and this event gives some helpfull params

 

private void dgAddTableDlgFieldsGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)

 

//You can obtain the current row datasource item from the grid

Entities.EntityTableFieldEntry currField = (Entities.EntityTableFieldEntry) e.Row.DataContext;

// You can  obtain the control associated to that cell

ListBox lbEdit = e.Column.GetCellContent(e.Row) as ListBox;

 

Now I have realized, that I have the problem of having ID / Description values... I think I will have to do more manual tweaking to display in the TextBlock the Description but keep the ID internally :-(.

 Thanks for your help

   Braulio 

 

// ---------------------------------
    Braulio Diez

    http://www.dbschemaeditor.com
    Free Silverlight Based Database Schema Editor
/// ---------------------------------

lee_sl
lee_sl

Contributor

Contributor

2992 points

585 Posts

Answered Question

Re: Re: Re: Re: DataBinding: using a listbox inside a DataGrid

look at DisplayMemberPath set Description to that property and when you get the selectedItem you get the complete object. you can get the id value from that. if you need more customization apply ItemTemplate to the listbox

----------------------------------------------
Available for consulting in Dallas, TX
http://leeontech.wordpress.com/

brauliod
brauliod

Participant

Participant

1169 points

472 Posts

Silverlight MVP

Re: Re: Re: Re: DataBinding: using a listbox inside a DataGrid

Thanks for the info, for the ListBox now is under control.

 For the textblock (non editing mode) I'm going to hook on the Loaded event for that control and set there the description value (manually search for the description associated with a given ID), maybe is not very elegant.

  Another possiblity could be to add to my entity ID plus description fields (something like Entity: car, color = 1 colorDesc=Red), but I would have to manually update both fields whenever there is a change.

 

// ---------------------------------
    Braulio Diez

    http://www.dbschemaeditor.com
    Free Silverlight Based Database Schema Editor
/// ---------------------------------

chinna_g
chinna_g

Member

Member

17 points

66 Posts

Re: Re: Re: Re: DataBinding: using a listbox inside a DataGrid

Hi,

I am looking for the same problem .... I am populating the data from dbase to the grid in silverlight2.0.

Now my requirement is when i select a row i need to get the updated/modified data for the particular row.... (with a hidden field like in asp.net 'DataKeyField i.e. if i select one row i need to get the EmpId for that particular row where as the empID will ot be visible to the user).

Please let me know the solution.....

obsidian3d
obsidian3d

Member

Member

9 points

20 Posts

Re: Re: Re: Re: Re: DataBinding: using a listbox inside a DataGrid

I don't see the .GetCellContent method on here anymore...has it been removed?

apple0124
apple0124

Member

Member

3 points

2 Posts

Re: Re: Re: Re: Re: DataBinding: using a listbox inside a DataGrid

SL 3.0 datagrid. I have created my CellEditingTemplate using a XAML. My GetCellContent(e.Row) gives the GridRow back not the combobox.

Here is some code

  public static DataTemplate returnComboBoxDataTemplate()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<DataTemplate " + DEFAULT_NAMESPACE + " " + WINDOWS_NAMESPACE + " " + APP_NAMESPACE + " " + WINFX_NS + " " + WINFX_BASIC + " " + WINDOWS_DATA_NAMESPACE + " >");
            sb.Append("<Grid>");
            sb.Append("<Grid.Resources>");
            sb.Append("<c:StaticPickListProvider x:Key=\"plp\"></c:StaticPickListProvider>");
            sb.Append("</Grid.Resources>");
            sb.Append("<ComboBox x:Name=\"myCbo\" Loaded=\"ComboBox_Loaded\" >");
            //sb.Append(">");
            sb.Append("</ComboBox>");
            sb.Append("</Grid>");
            sb.Append("</DataTemplate>");
            return (DataTemplate)XamlReader.Load(sb.ToString());
        }

When I do

     void parentGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
        {
            DataGrid dg = (DataGrid)sender;
            ComboBox cboItem= (ComboBox)dg.CurrentColumn.GetCellContent(e.Row);
            cboItem.ItemsSource = _pickListItems;
            cboItem.SelectedIndex = 0;

          MessageBox.Show( e.Column.DisplayIndex.ToString());
        }

 

GetCellContent gives me the Grid.Row.

How do I get the combobox?

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities