Accessing EditingElement in DataGridTemplateColumn

Nov 5, 2008 at 8:26 PM
I've got the following DataGrid with a DataGridTemplateColumn.

                <dg:DataGrid HorizontalAlignment="Stretch" Margin="8,34,8,8"
                             VerticalAlignment="Stretch" Width="Auto" Height="Auto"
                             x:Name="dgProfitMargins" Style="{StaticResource dgStyle}"
                             ColumnHeaderStyle="{StaticResource dgColumnHeaderStyle}"
                             CellEditEnding="dgProfitMargins_CellEditEnding">
                    <dg:DataGrid.ItemContainerStyle>
                        <Style TargetType="{x:Type dg:DataGridRow}">
                            <EventSetter Event="UIElement.PreviewKeyDown" Handler="dgrProfitMargins_PreviewKeyDown"/>
                        </Style>
                    </dg:DataGrid.ItemContainerStyle>
                    <dg:DataGrid.Columns>
                        <dg:DataGridTemplateColumn Header="Start" Width="100">
                            <dg:DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Lower, Converter={StaticResource cvtCurrencyUSConverter}}" TextAlignment="Right"/>
                                </DataTemplate>
                            </dg:DataGridTemplateColumn.CellTemplate>
                            <dg:DataGridTemplateColumn.CellEditingTemplate>
                                <DataTemplate>
                                    <tc:NumericTextBox Value="{Binding Lower}" DataType="Decimal" FormatType="Currency" BorderThickness="0" Padding="0"/>
                                </DataTemplate>
                            </dg:DataGridTemplateColumn.CellEditingTemplate>
                        </dg:DataGridTemplateColumn>
                        ....

        private void dgProfitMargins_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            if (e.EditingElement == null) return;
            if (e.EditingElement.DataContext == null) return;
            
            InventoryProfitMargin oldRate = (InventoryProfitMargin)e.EditingElement.DataContext;
            NumericTextBox ntb = (NumericTextBox)e.EditingElement;

            switch (e.Column.Header.ToString())
            {
            ....

When I access e.EditingElement in dgProfitMargins_CellEditEnding, it is of type System.Windows.Controls.ContentPresenter, not NumericTextBox. How can I gain access to the NumericTextBox control?
Coordinator
Nov 5, 2008 at 9:23 PM
Search the visual tree for the NumericTextBox in the children of ContentPresenter.  You should be able to find it.