tags:

views:

257

answers:

1

I want to create a Custom Control derived from ComboBox, however if I create custom control from visual studio template, it creates a default style of Control and I have to then open MSDN's control template of ComboBox and then recreate entire combobox style once again an in generic.xaml

What I want to do is, I want to create a custom control derived from ComboBox and in the generic.xaml I only want to define ItemTemplate and not entire ControlTemplate.

However if I keep the line

static MyComboBox()
{
    DefaultStyleKeyProperty.
         OverrideMetadata(typeof(MyComboBox), 
         new FrameworkPropertyMetadata(
             typeof(MyComboBox)));
}

Then I dont see anything at all if I remove the ControlTemplate from generic.xaml, however if I define key ItemTemplate in generic.xaml, how should I initialize my static constructor in order to refelct only ItemTemplate?

Sure I can redesign the xaml theme from msdn help but isnt there any easy way to do it?

+1  A: 

Hey Akash,

I recently wrote my own custom combobox that only changes the ItemTemplate of the control. The rest of the combobox style is inherited from the default combobox.

In your custom control constructor:

 static CustomComboBox()
 {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomComboBox)
                        , new FrameworkPropertyMetadata(typeof(CustomComboBox)));

 }

And then in your generic.xaml:

<Style TargetType="{x:Type local:CustomComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                **INSERT YOUR ITEM TEMPLATE HERE**
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

The key here is setting the style's BasedOn property to reference the standard combobox control.

Hope this helps!

stevosaurus