0

In a DataGrid I display data about financial transactions. One column is the Account with the xaml below. The CellEditingTemplate display the ComboBox with the accounts. The CellTemplate is tricky. I want to display the Account as a TextBlock. Previously I bound "Account.Name" but validating a nested property is not easy if Account is null (e.g. in a new row). So I decided to use ComboBox but with a ControlTemplate. Displaying values and validation works. The problem is that for click the CellEditingTemplate is not activated.

            <DataGridTemplateColumn Header="{x:Static r:Resource.AccountName}">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, 
                            AncestorType={x:Type DataGrid}}, Path=DataContext.AccountObjects}" 
                                  SelectedItem="{Binding Account, ValidatesOnDataErrors=True}" 
                                  DisplayMemberPath="Name">
                            <ComboBox.Template>
                                <ControlTemplate TargetType="{x:Type ComboBox}">
                                    <TextBlock Text="{TemplateBinding Text}"/>
                                </ControlTemplate>
                            </ComboBox.Template>
                        </ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, 
                            AncestorType={x:Type DataGrid}}, Path=DataContext.AccountObjects}" 
                                  SelectedItem="{Binding Account, ValidatesOnDataErrors=True}" 
                                  DisplayMemberPath="Name"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
Istvan Heckl
  • 864
  • 10
  • 22
  • Can you check output window for binding errors. DataGridComboBoxColumn column type doesn't your case? – neelesh bodgal May 21 '20 at 13:25
  • @neeleshbodgal There is no binding error in the output window. The problem is that mouse click is not handled properly. – Istvan Heckl May 21 '20 at 14:17
  • what do u mean by "mouse click is not handled properly".? code what u have written is fine, in CellTemplate if u need to display value in textblock then directly u can bind to textblock – neelesh bodgal May 21 '20 at 14:45
  • @neeleshbodgal For mouse click the CellEditingTemplate should be active which should display the ComboBox so a new item can be selected. – Istvan Heckl May 21 '20 at 15:18
  • Please check this [post](https://stackoverflow.com/questions/31099169/datagridtemplatecolumn-combobox-datepicker-resets-clears-and-doesnt-fire-add), it might be helpful – neelesh bodgal May 22 '20 at 13:21
  • @neeleshbodgal Hi, I checked the post, tried some idea but it did not help. I have ComboBox which is displayed as a TextBlock (that is what I want), but clicking it does not activate the CellEditingTemplate. – Istvan Heckl May 22 '20 at 14:58

0 Answers0