Custom CellType or Control in Datagrid

Apr 14, 2009 at 9:38 PM
I need to create a cell that would work much like a combobox, but instead of a listbox dropping down, it would show a popup form.  When the user selects a item in the popup form, the text needs to be displayed but the value is persisted as well to the cell, since that is what the cell would be bound to in the database.

I need to do it with a popup because I need to be able to do more than what a combobox does.  I need a full form.

Any suggested on how to persist the selected item back to a cell and storing the text/value would be appreciated.
Coordinator
Apr 15, 2009 at 12:55 AM
Just so you know, you can template ComboBoxItems to be virtually anything you want.  If that can meet your needs then you'll get the rest of the functionality you describe for free from the DataGridComboBoxColumn.
Apr 15, 2009 at 2:11 AM
Thanks for the response.  I did actually create a style for a combobox that does what I need.  I just can't figure out how to apply it to a DataGridComboBoxColumn
Coordinator
Apr 15, 2009 at 3:42 AM
See this post, http://blogs.msdn.com/vinsibal/archive/2009/04/07/5-random-gotchas-with-the-wpf-datagrid.aspx.  In particular, number 4 about the ElementStyle and EditingElementStyle.
Apr 15, 2009 at 4:54 AM
Thanks for pointing that out.  I think I resolved it but I am not sure why.  I had created a resource and if I set it like this, it doesn't work:

ElementStyle

 

="{DynamicResource ComboBoxStyle1}"

 

But if I put the contents of ComboBoxStyle1 inside the control like this, it does work

<

 

Custom:DataGridComboBoxColumn.ElementStyle>

 

<

 

Style

 

 

TargetType="{x:Type ComboBox}">

 

 

 

<Setter

 

 

Property="FocusVisualStyle"

 

 

Value="{StaticResource ComboBoxFocusVisual}" />

 

....(Left out all because it is alot of XAML)

</

 

Custom:DataGridComboBoxColumn.ElementStyle>

 



So I am not sure why it has to be inline like that.

Coordinator
Apr 15, 2009 at 12:46 PM
The issue you are seeing with the DynamicResource is the issue described in this thread, http://wpf.codeplex.com/Thread/View.aspx?ThreadId=50379.
Apr 15, 2009 at 5:19 PM
Edited Apr 15, 2009 at 5:50 PM
Very helpful.  I am almost there.  Now, here is the only issue I am having left.  My dropdown is now a Datagrid with two buttons at the bottom.  Cancel and Select.  When I hit Select, I need to assign the selected text/value to the text portion of the combobox and close the dropdown.  Cancel just closes the dropdown.

Any help on how I do that?
Coordinator
Apr 16, 2009 at 1:30 AM
This post, http://blogs.msdn.com/vinsibal/archive/2009/04/10/launching-a-custom-dialog-to-edit-items-in-a-datagrid-with-mvvm.aspx, talks about launching a form to edit a row.  Maybe that can give you some ideas on how to do that with just a cell.
Apr 23, 2009 at 1:56 AM

How do you template the combobox items in a DataGridComboBoxColumn?  In guess I didn't think about that when you first responded.  I dont see an ItemTemplate for it.

 

Coordinator
Apr 23, 2009 at 5:13 AM
You will also have to do that through the ElementStyle and EditingElementStyle.

<DataGridComboBoxColumn.ElementStyle>
<Style TargetType=ComboBox>
<Setter Property="ItemTemplate">...</ >
Apr 23, 2009 at 6:57 AM
Thanks for the info.  I plugged that in but get an error says

Error 1 Cannot resolve the Style Property 'ItemTemplate'. Verify that the owning type is the Style's TargetType, or use Class.Property syntax to specify the Property. Line 500 Position 21. C:\MyStuff\Alexai\WpfApplication2\WpfApplication2\Window1.xaml 500 21 WpfApplication2


Here is the code for that one column


<dg:DataGridComboBoxColumn x:Name="colColumnType" Header="ColumnType" Width="100" SelectedItemBinding="{Binding ListCategoryID}" >
                    <dg:DataGridComboBoxColumn.ElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="ItemTemplate">
                                <Setter.Value>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <TextBox Text="(Binding ListCategoryID}"></TextBox>
                                            <TextBox Text="(Binding CategoryName}"></TextBox>
                                        </StackPanel>
                                    </DataTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </dg:DataGridComboBoxColumn.ElementStyle>
                </dg:DataGridComboBoxColumn>

 

 

Coordinator
Apr 23, 2009 at 10:38 PM
I think it should be on EditingElementStyle, not ElementStyle.  Try that out and let me know if you still have issues.