datagrid celleditingtemplateselector sender=null always

Dec 19, 2009 at 4:02 AM

 

I have a datagrid which has a rowdetailstemplate that holds another datagrid. I can use celltemplateselector and celleditingtemplateselector to load a 'default' template for a datagridtemplatecolumn of this second datagrid. I would like to use the celleditingtemplateselector to determine if the underlying item has a certain value (i.e. number versus bool). and select a different template according to value etc.

The problem I have is the following code has sender always as null and therefore I cannot cast to the underlying object:

 

class UserCell : DataTemplateSelector
    {
        public override DataTemplate
            SelectTemplate(object item, DependencyObject container)item is null despite template working otherwise correctly!!
        {
            if (item != null && item is Guideline_node)** always null
            {
                Guideline_node test = item as Guideline_node;
                Window window = Application.Current.MainWindow;

                return
                   window.FindResource("usercell")
                   as DataTemplate;
            }
            else return window.FindResource("usercell2")
                   as DataTemplate;

        }

    }

 

the xaml for the main grid and rowdetailstemplategrid is :

 

									<Custom:DataGrid.Columns>
										<Custom:DataGridCheckBoxColumn Header="Is True" Binding="{Binding ruleIsTrue}" Visibility="Collapsed"/>
										<Custom:DataGridTextColumn  Header="Condition" Binding="{Binding MedCon}" FontSize="16" FontWeight="Bold" />
										<Custom:DataGridTextColumn Header="Rule" Binding="{Binding Rulename}" FontSize="13.333" FontStyle="Italic">
											<Custom:DataGridTextColumn.Foreground>
												<SolidColorBrush Color="{DynamicResource Panelbase}"/>
											</Custom:DataGridTextColumn.Foreground>
										</Custom:DataGridTextColumn>
										<Custom:DataGridTextColumn/>
										<Custom:DataGridComboBoxColumn/>
									</Custom:DataGrid.Columns>
									<Custom:DataGrid.RowDetailsTemplate>	
										<DataTemplate>
											<Custom:DataGrid x:Name="RulelistingPtSub" AutoGenerateColumns="False" Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DataContext="{Binding Mode=OneWay, Source={StaticResource Observable_Guideline_libraryDataSource}}" GridLinesVisibility="None"  SelectionChanged="RulelistingPtSub_SelectionChanged"  BorderBrush="{x:Null}" HorizontalGridLinesBrush="{x:Null}" VerticalGridLinesBrush="{x:Null}" RowBackground="{x:Null}" Opacity="1" ContextMenuClosing="RulelistingPtSub_ContextMenuClosing" RowStyle="{DynamicResource DataGridRowStyle1}" HeadersVisibility="Column" CellStyle="{DynamicResource DataGridCellStyle1}" >
												<Custom:DataGrid.AlternatingRowBackground>
													<SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ControlLightColorKey}}"/>
												</Custom:DataGrid.AlternatingRowBackground>
												<Custom:DataGrid.Foreground >
													<SolidColorBrush Color="{DynamicResource ppt_text}"/>
												</Custom:DataGrid.Foreground>
												<Custom:DataGrid.ContextMenu>
													<ContextMenu>
														<MenuItem Header="Disable clinical state" />
													</ContextMenu>
												</Custom:DataGrid.ContextMenu>
												<Custom:DataGrid.Columns>
													<Custom:DataGridTextColumn Header="Connect" Binding="{Binding guidelineConnectLogic}">
														<Custom:DataGridTextColumn.Foreground>
															<SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ControlDarkColorKey}}"/>
														</Custom:DataGridTextColumn.Foreground>
													</Custom:DataGridTextColumn>
													<Custom:DataGridTextColumn Header="Clinical State" Binding="{Binding guidelineDependentObject}" IsReadOnly="True" Width="250" />
											****at this line<Custom:DataGridTemplateColumn x:Name="UserInputCell" Header="template" IsReadOnly="False" CellEditingTemplateSelector="{Binding Mode=OneWay, Source={StaticResource UserCellEditDataSource}}" CellTemplateSelector="{Binding Mode=OneWay, Source={StaticResource UserCellDataSource}}" />
													<Custom:DataGridTextColumn Binding="{Binding guidelineValueDB}" Header="Patient value" FontStyle="Normal" Width="150" FontWeight="Bold"/>
													<Custom:DataGridTextColumn Header="Unit" Binding="{Binding guidelineUnit}" FontStyle="Normal" IsReadOnly="True"/>
													<Custom:DataGridTextColumn Header="Date" Binding="{Binding guidelineDateDB}" IsReadOnly="True"/>
													<Custom:DataGridTextColumn Header="Assess" Binding="{Binding guidelineEqualsLogic}" IsReadOnly="True"/>
													<Custom:DataGridTextColumn Header="Value" Binding="{Binding guidelineValue}" IsReadOnly="True"/>
													<Custom:DataGridTextColumn Header="Date dep" Binding="{Binding guidelineDate}" IsReadOnly="True"/>
													<Custom:DataGridTextColumn Header="Is true?" Binding="{Binding guidelineIsCorrect}" IsReadOnly="True" Visibility="Visible"/>
													<Custom:DataGridTextColumn Header="Missing mapping data" Binding="{Binding isMissingLookupOrData}" IsReadOnly="True" Visibility="Visible" />
													<Custom:DataGridTextColumn Header="Disabled for patient" Binding="{Binding isDeactivatedForpt}" Visibility="Visible"/>
												</Custom:DataGrid.Columns>
											</Custom:DataGrid>
											<DataTemplate.Triggers>
												<Trigger Property="ContentControl.Content" Value="{x:Null}"/>
											</DataTemplate.Triggers>	
										</DataTemplate>
									</Custom:DataGrid.RowDetailsTemplate>
									<Custom:DataGrid.Foreground>
										<SolidColorBrush Color="{DynamicResource Panelbase}"/>
									</Custom:DataGrid.Foreground>
								</Custom:DataGrid>

 

Can anyone help me with this??

 

 

 

 

Dec 21, 2009 at 8:17 PM

For those interested, I posted my own solution at:

http://social.msdn.microsoft.com/Forums/en/wpf/thread/b47ac38a-077f-41da-99b1-8b88add693d8