How to inherit RadComboBox ?

5 posts, 1 answers
  1. Arnaud
    Arnaud avatar
    25 posts
    Member since:
    Jan 2013

    Posted 12 Mar 2015 Link to this post

    Hello
    I would create my own RadComboBox (for adding features) and add a button at right (see capture)
    I create a control inherit of RadComboBox :
    public class MyComboBox : Telerik.Windows.Controls.RadComboBox
    {
     
    }
    And associate style :
    <Style x:Key="MyComboBoxStyle" TargetType="Controls:MyComboBox">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Controls:MyComboBox" >
                        <Grid x:Name="LayoutRoot">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
     
                            <telerik:RadComboBox x:Name="PART_RadComboBox" Grid.Column="0"
                                                 ItemsSource="{TemplateBinding ItemsSource}"
                                                 SelectedItem="{TemplateBinding SelectedItem}"
                                                 SelectedValue="{TemplateBinding SelectedValue}"
                                                 DisplayMemberPath="{TemplateBinding DisplayMemberPath}"
                                                 SelectedValuePath="{TemplateBinding SelectedValuePath}">
                            </telerik:RadComboBox>
     
                            <telerik:RadButton x:Name="PART_RadButton" Grid.Column="1" Content="+"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    But MyComboBoxStyle is never use, I think RadComboBox always override my style because if I inherit System.Windows.ComboBox it's work.
    Can you help me ? Or I must create UserControl and rewrite all DependencyProperty of RadComboBox ? 
    Thanks in advance

    PS : I use Telerik 2014 Q2.
  2. Kalin
    Admin
    Kalin avatar
    1207 posts

    Posted 16 Mar 2015 Link to this post

    Hello Arnaud,

    You could either inherit from RadComboBox, add the needed properties and extract and modify the default Style and Template of the control in order to add the button. Afterwards you would need to apply the Style with TargetType the new control.

    Or you can simply create new UserControl, add RadComboBox and the Button as content. In the code behind you can implement the needed functionality without applying any Styles and Templates.

    Hope this helps.

    Regards,
    Kalin
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Arnaud
    Arnaud avatar
    25 posts
    Member since:
    Jan 2013

    Posted 18 Mar 2015 Link to this post

    I would inherit RadComboBox but without extract Style and Template, only use a Style BasedOn RadComboBoxStyle
    I try with System.Controls.ComboBox and it's work but not with RadComboBox. 
    MyComboBox.cs (very simple)
    public class MyComboBox : Telerik.Windows.Controls.RadComboBox
    {
    }
     
    public class MyComboBox2 : System.Windows.Controls.ComboBox
    {
    }
    MyComboBox.xaml
    <Style x:Key="MyComboBoxStyle" TargetType="Controls:MyComboBox">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Controls:MyComboBox" >
                        <Grid x:Name="LayoutRoot">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
     
                            <telerik:RadComboBox x:Name="PART_RadComboBox" Grid.Column="0"
                                                 ItemsSource="{TemplateBinding ItemsSource}"
                                                 SelectedItem="{TemplateBinding SelectedItem}"
                                                 SelectedValue="{TemplateBinding SelectedValue}"
                                                 DisplayMemberPath="{TemplateBinding DisplayMemberPath}"
                                                 SelectedValuePath="{TemplateBinding SelectedValuePath}">
                            </telerik:RadComboBox>
     
                            <telerik:RadButton x:Name="PART_RadButton" Grid.Column="1" Content="+"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
     
        <Style x:Key="MyComboBoxStyle2" TargetType="Controls:MyComboBox2">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Controls:MyComboBox2" >
                        <Grid x:Name="LayoutRoot">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
     
                            <ComboBox x:Name="PART_RadComboBox" Grid.Column="0"
                                                 ItemsSource="{TemplateBinding ItemsSource}"
                                                 SelectedItem="{TemplateBinding SelectedItem}"
                                                 SelectedValue="{TemplateBinding SelectedValue}"
                                                 DisplayMemberPath="{TemplateBinding DisplayMemberPath}"
                                                 SelectedValuePath="{TemplateBinding SelectedValuePath}">
                            </ComboBox>
     
                            <telerik:RadButton x:Name="PART_RadButton" Grid.Column="1" Content="+"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    And my View
    <Label Grid.Row="0" Content="Telerik.Windows.Controls.RadComboBox"/>
            <Control:MyComboBox Grid.Row="1" Margin="5" Style="{StaticResource MyComboBoxStyle}"
                                 ItemsSource="{Binding ListItems, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
                                 DisplayMemberPath="Item2" SelectedValuePath="Item1"
                                 SelectedValue="{Binding Entite.IdSubItem, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
     
            <Label Grid.Row="2" Content="System.Windows.Controls.ComboBox"/>
            <Control:MyComboBox2 Grid.Row="3" Margin="5" Style="{StaticResource MyComboBoxStyle2}"
                                 ItemsSource="{Binding ListItems, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
                                 DisplayMemberPath="Item2" SelectedValuePath="Item1"
                                 SelectedValue="{Binding Entite.IdSubItem, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
    As you can see in my screenshot, with ComboBox, my button appear but not with RadComboBox, why ??
    Thanks in advance for help
  5. Answer
    Kalin
    Admin
    Kalin avatar
    1207 posts

    Posted 20 Mar 2015 Link to this post

    Hi Arnaud,

    RadComboBox uses the NonEditableTemplate and EditableTemplate properties. So if you change the Setter to NonEditableTemplate - it will appear the same way as the regular ComboBox:

    <Style x:Key="MyComboBoxStyle" TargetType="Controls:MyComboBox">
    <Setter Property="NonEditableTemplate">

    Hope this helps.

    Regards,
    Kalin
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  6. Arnaud
    Arnaud avatar
    25 posts
    Member since:
    Jan 2013

    Posted 23 Mar 2015 in reply to Kalin Link to this post

    Thanks very much
    I don't remember RadComboBox doesn't have one Template but two templates.
    That's work great. 
    Regards
Back to Top
UI for WPF is Visual Studio 2017 Ready