DataGridComboBoxColumn bound to LINQ to SQL classes

May 17, 2009 at 9:58 AM
Edited May 17, 2009 at 10:29 AM

Hello everybody!

I need a little help with DataGridComboBoxColumn bound to Table<T> generated with VisualStudio O/R Designer. Lets say there are two classes Article and ArticleType and because of the foreign key relation in Article class ArticleType1 property is generated. I use it to expose to article some of article type values. In simple scenario with this binding all works fine:

 <dg:DataGridComboBoxColumn Header="Article Type" x:Name="articleTypesColumn" ItemsSource="{Binding}"

SelectedItemBinding="{Binding Path=ArticleType1, Mode=TwoWay}" DisplayMemberPath="Value">

In code behind I set ItemsSource to ArticleTypes property of my DataContext object db:

articleTypesColumn.ItemsSource = db.


Problem occurs when I set EditingElementStyle="{StaticResource comboBoxStyle}"  in order to use styled combo box. I just change visualization in couple elements in the original ControlTemplate visual tree. To extract the original style element I'm using Expression Blend3.

Then in edit mode it seems like binding is no longer valid and in my combo I can see just items class name: SampleApp.ArticleType.

Do I need to use DataGridTemplateColumn to resolve this problem?


May 17, 2009 at 8:17 PM

For anyone fallen in the same problem: I found that binding of ContentPresenter for DataGridComboBoxColumn has set one "extra" property - ContentTemplateSelector:

ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"

And that was the key. It seems that if Template property is set in style, ContentPresenter must follow the model from its “base” style.


<ContentPresenter Margin="1,1,1,1"

                  Content="{TemplateBinding SelectionBoxItem}"

                  ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"

                  ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"

                  ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}"

                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"

                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"

                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"

                  IsHitTestVisible="false" />

Let me know if I’m wrong or this is the right way to styling DataGridComboBoxColumn.