Binding DataGridComboBoxColumn without using Converter

Oct 3, 2008 at 3:32 AM

Vincent Sibal's article "WPF DataGrid: Working with DataGridComboBoxColumns (Part 2)" explains how to bind DataGridComboBoxColumn using Converter (http://blogs.msdn.com/vinsibal/archive/2008/08/28/wpf-datagrid-working-with-datagridcomboboxcolumns-part-2.aspx). For those, who don't like to create Converter for every ComboBox, I have a temporary solutions until DataGrid final release comes out.

Here are the couple of ideas which came to my mind:
1. Use DataGridTemplateColumn overriding DataGridTemplateColumn.CellTemplate. The idea is to write the same ComboBox definition in two places: CellTemplate and CellEditingTemplate. The only difference between these two definitions is attribute IsEnabled="False" inside the first one. In this case you just see disabled ComboBox control instead of default TextBlock:

 

<dg:DataGridTemplateColumn Header="CustomerID (ContactName alias)">

    <dg:DataGridTemplateColumn.CellTemplate>

        <DataTemplate>

            <ComboBox IsEnabled="False" ItemsSource="{Binding Source={StaticResource customerDataProvider}}" SelectedValue="{Binding Path=CustomerID}" SelectedValuePath="CustomerID" DisplayMemberPath="ContactName" />

        </DataTemplate>

    </dg:DataGridTemplateColumn.CellTemplate>

    <dg:DataGridTemplateColumn.CellEditingTemplate>

        <DataTemplate>

            <ComboBox ItemsSource="{Binding Source={StaticResource customerDataProvider}}" SelectedValue="{Binding Path=CustomerID}" SelectedValuePath="CustomerID" DisplayMemberPath="ContactName" />

        </DataTemplate>

    </dg:DataGridTemplateColumn.CellEditingTemplate>

</dg:DataGridTemplateColumn>



2. This case is similar to the first one with the only difference that NonExpandableCombobox style simulates default TextBlock appearance:

<Window.Resources>

    <Style x:Key="NonExpandableComboBox" TargetType="ComboBox">

        <Setter Property="OverridesDefaultStyle" Value="true"/>

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="ComboBox">

                    <Grid>

                        <ContentPresenter Content="{TemplateBinding SelectionBoxItem}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" />

                    </Grid>

                </ControlTemplate>

            </Setter.Value>

        </Setter>

    </Style>

</Window.Resources>

<dg:DataGridTemplateColumn Header="CustomerID (ContactName alias)">

    <dg:DataGridTemplateColumn.CellTemplate>

        <DataTemplate>

             <ComboBox DisplayMemberPath="ContactName" IsEnabled="False" ItemsSource="{Binding Source={StaticResource customerDataProvider}}" SelectedValue="{Binding Path=CustomerID}" SelectedValuePath="CustomerID" Style="{StaticResource NonExpandableComboBox}" />

        </DataTemplate>

    </dg:DataGridTemplateColumn.CellTemplate>

    <dg:DataGridTemplateColumn.CellEditingTemplate>

        <DataTemplate>

            <ComboBox ItemsSource="{Binding Source={StaticResource customerDataProvider}}" SelectedValue="{Binding Path=CustomerID}" SelectedValuePath="CustomerID" DisplayMemberPath="ContactName" />

        </DataTemplate>

    </dg:DataGridTemplateColumn.CellEditingTemplate>

</dg:DataGridTemplateColumn>