MyComboBox vs. MyComboBoxItem

3 posts, 1 answers
  1. Richard
    Richard avatar
    17 posts
    Member since:
    Nov 2008

    Posted 09 Jun 2009 Link to this post

    Hi. First let me tell you what I'm trying to achieve. Basically, I'm trying to set up some conventions at our company to put some consistency on the table. The RadControls are excellent, but I want to have our own controls that inherit directly from the RadControls. For example, MyComboBox inherits from RadComboBox and MyComboBoxItem inherits from RadComboBoxItem, like so...

    MyComboBox.xaml:

    <telerik:MyComboBox x:Name="Sandbox.MyComboBox" FontFamily="./Fonts.zip#Calibri" FontSize="13" Height="22"...

    MyComboBox.xaml.cs

    public partial class MyComboBox : RadComboBox { ... }

    The same technique is used for the RadComboBoxItem. I've also applied custom templates (not styles) to define how they should be look. This way, even without cascading styles (waiting for SL3), I can define a default appearance that can be overridden by styles. Furthermore, I can't make the mistake of setting the height of a RadComboBox to 24 later on down the line because it will be overridden by MyComboBox's properties, which is exactly what I want to happen in this case. If Silverlight 2 handled cascading styles I probably wouldn't need any of this, but we have to wait for SL3 for that, don't we?

    Now, here's where my question comes in. I've bound data to the MyComboBox and although I have my own MyComboBoxItem controls, the RadComboBox control automatically binds data with the RadComboBoxItem. I've specifically designed MyComboBoxItem to show a highlighted color on MouseOver, but it doesn't show because I can't figure out how to get MyComboBoxItem to bind to the RadComboBox instead of the RadComboBoxItem.

    Do I have to start from scratch and create my own Telerik theme instead? Is there a way I can bind data to MyComboBoxItem without losing functionality? I lost functionality when I tried this:

    <Sandbox:MyComboBox x:Name="MyCombo" ItemsSource="{StaticResource customers}"
        <Sandbox:MyComboBox.ItemTemplate> 
            <DataTemplate> 
                <Sandbox:MyComboBoxItem Content="{Binding Content}"/> 
            </DataTemplate> 
        </Sandbox:MyComboBox.ItemTemplate> 
    </Sandbox:MyComboBox> 

    With the above example, the ComboBoxItems would no longer respond to mouse clicks. Weird!
  2. Answer
    Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 09 Jun 2009 Link to this post

    Hi Alex,

    When you want to inherit from both an ItemsControl and its items, you should override the GetContainerForItemOverride method, that should return an instance of your item class. In your specific case - ESOComboBoxItem. This way the inherited RadComboBox will automatically generate  the inherited ESOComboBoxItem instead of the base RadComboBoxItem and you will not need to put items in the DataTemplate, which will not work anyway.

    public partial class MyComboBox : RadComboBox
    {
        protected override DependencyObject GetContainerForItemOverride() 
        { 
            return new MyComboBoxItem(); 
        } 
        ...
    }

    public partial class MyComboBoxItem : RadComboBoxItem
    {
    }

    Best wishes,
    Valeri Hristov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Richard
    Richard avatar
    17 posts
    Member since:
    Nov 2008

    Posted 09 Jun 2009 Link to this post

    OMG that's so easy! I think I love you...

    THANK YOU!!1!
Back to Top