Search code examples
c#wpftelerik-grid

Create a DataTemplate for a RadGridViewComboBox Column


instead of using the default telerik gridviewcombobox template, I want to override it and use the wpf original combobox. It works completely fine before trying to apply the datatemplate.

<Telerik:GridViewComboBoxColumn 
    Header="Status" 
    DataMemberBinding="{Binding Status_Id}" 
    ItemsSource="{Binding Statuses, Mode=TwoWay}" 
    DisplayMemberPath="StatusName"  
    SelectedValueMemberPath="Id">
</Telerik:GridViewComboBoxColumn>

When I try to apply the datatemplate, the combo box now displays blank values.

 <Telerik:GridViewComboBoxColumn Header="Status"
    <Telerik:GridViewComboBoxColumn.CellTemplate>
        <DataTemplate>
            <ComboBox SelectedValue="{Binding Status_Id}" 
                      ItemsSource="{Binding Statuses, Mode=TwoWay}" 
                      DisplayMemberPath="StatusName" 
                      SelectedValuePath="Id">
            </ComboBox>
        </DataTemplate>
    </Telerik:GridViewComboBoxColumn.CellTemplate>
</Telerik:GridViewComboBoxColumn>

Am I setting the selected value properties value incorrect? Any assistance would be greatly appreciated. I think when I set the datatemplate, it's hitting the wrong layer. I don't think it's grabbing Statuses from the Viewmodel anymore.


Solution

  • Here is my template I'm using in my projects:

    Data Template

                    <telerik:GridViewDataColumn Width="150" DataMemberBinding="{Binding Path=StackOptimizerSelectedRule}"
                                            Header="Rules"
                                            IsFilterable="False" IsReorderable="False">
                    <telerik:GridViewDataColumn.CellTemplate>
                        <DataTemplate DataType="flowConfiguration:StackOptimizerParameterRuleTreeViewModel">
                            <TextBlock Text="{Binding StackOptimizerSelectedRule, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource EnumTypeConverterKey}}"></TextBlock>
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellTemplate>
                    
                    <telerik:GridViewDataColumn.CellEditTemplate>
                        <DataTemplate DataType="flowConfiguration:StackOptimizerParameterRuleTreeViewModel">
                            <ComboBox 
                                ItemsSource="{Binding Source={StaticResource StackOptimizerSelectionRules}}"
                                SelectedItem="{Binding StackOptimizerSelectedRule, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                                <ComboBox.ItemTemplate>
                                    <DataTemplate DataType="flowConfiguration:StackOptimizerParameterRuleTreeViewModel">
                                        <TextBlock Text="{Binding Converter={StaticResource EnumTypeConverterKey}, UpdateSourceTrigger=PropertyChanged}"/>
                                    </DataTemplate>
                                </ComboBox.ItemTemplate>
                            </ComboBox>
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellEditTemplate>
                </telerik:GridViewDataColumn>
    

    Explanations

    Here are two templates. The GridViewDataColumn.CellTemplate will be available when containing cell is not in a focus. The CellEditTemplate will be available when a containing cell is in a focus and user change his selection.

    Please keep in your mind next things, you have several ways to bound the ItemsSource of the combo:

    1. Regular binding ItemsSource="{Binding SourceCollection, UpdateSourceTrigger=PropertyChanged}". Use this way when your SourceCollection is presenting in the Cell DataContext.
    2. Relative binding ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type PutHereTheTypeOfActualParentThatHoldsDataContextYouNeed}}, Path=DataContext.SourceCollection}". Use this way when your SourceCollection is inside the Parent's data context.
    3. Source from xaml ItemsSource="{Binding Source={StaticResource SourceCollection}}". Use this way when your SourceCollection is static collection Generated in Xaml(for example; based on enum types). You need the next declaration in your <SomeParentVisualAccessibleByridViewDataColumn.Resource> section.

    Declaration of source for the third (in addition read the next article)

    <ObjectDataProvider x:Key="SourceCollection"
                                MethodName="GetValues"
                                ObjectType="{x:Type flowConfiguration:StackOptimizerSelectionRules}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="flowConfiguration:StackOptimizerSelectionRules"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    

    In my opinion your problem is an incorrect combo's ItemsSource biding, check if you have related binding error exception in your output window. Let me know if you need any help.

    Regards.