WPF Data Gird - DataGridTemplateColumn Question

Apr 28, 2009 at 6:27 PM
Edited Apr 28, 2009 at 6:28 PM
Hi,

I am trying to use DataGridTemplateColumn in my data grid and populating a combo box inside the Grid. The purpose of the Grid is to let the user create a filter in order to extract certain data:

 <my:DataGrid Name="dataGrid2"
                     SelectionUnit="CellOrRowHeader"
                     IsSynchronizedWithCurrentItem="True"
                     HeadersVisibility="All"
                     Background="Transparent"
                     RowHeaderWidth="25"
                     SelectionMode="Extended"
                     RowBackground="White"
                     AlternatingRowBackground="LightGray"   
                     AutoGenerateColumns="False"
                     AlternationCount="2" Margin="0,161,0,-161"
                     ItemsSource="{Binding}">
            <my:DataGrid.Columns>
                <my:DataGridTextColumn Header="Filter Name" DataFieldBinding="{Binding Path=FilterName}"/>
                <my:DataGridTemplateColumn Header="(">
                    <my:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox IsEditable="False" Name="cb1" > <!--ItemsSource="{Binding Source={StaticResource MyFilterCol}}" Text="{Binding FilterName}" DisplayMemberPath="FilterName">-->
                                <ComboBoxItem>(</ComboBoxItem>
                            </ComboBox>
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellEditingTemplate>
                </my:DataGridTemplateColumn>
                <my:DataGridTemplateColumn Header="And-Or">
                    <my:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox>
                                <ComboBoxItem>AND</ComboBoxItem>
                                <ComboBoxItem>OR</ComboBoxItem>
                            </ComboBox>
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellEditingTemplate>
                </my:DataGridTemplateColumn>
                
                <my:DataGridComboBoxColumn Header="Field"/>
                
                <my:DataGridComboBoxColumn Header="Operator"/>
                <my:DataGridComboBoxColumn Header="Value"/>
                <my:DataGridComboBoxColumn Header=")"/>
            </my:DataGrid.Columns>
        </my:DataGrid>


What I have noticed is that when the Grid loads and I select something from the combo-box as soon as I change the focus the data goes away. What could be the possible fix for this?
I have even noticed that simple Textblocks that are placed within the DataGridTemplateColumn don't show any data until I click on the cell itself.
The following code is what I am talking about:


<my:DataGridTemplateColumn Header="Filter Name">
                    <my:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=FilterName}"/>
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellEditingTemplate>
 </my:DataGridTemplateColumn>


Coordinator
Apr 28, 2009 at 8:45 PM
When a DataGridCell has focus, the CellEditingTemplate is used, but when the cell does not have focus, the CellTemplate is used. In your ComboBox case, you will need to bind the selection to something else so that when the ComboBox goes away, it will persist.

Ben