bl4kh4k
bl4kh4k

Reputation: 1440

Place a ContentControl inside a custom control XAML

I created a custom control and what to use a ContentControl inside that control for use of the MVVM design pattern however my control does not like this when I run the application. For testing I also tried other standard controls but none of them work inside the custom control... just more custom controls that are dependant on the parent custom control.

Does anybody suggest how to place standard controls such as the ContentControl inside a custom control?

Cheers.

EDIT

XamlParseException -> 'Add value to collection of type 'System.Collections.ObjectModel.ObservableCollection(Ribbon_Framework.RibbonTabItem)' threw an exception.' Line number '8' and line position '14'.

    <Ribbon:Ribbon AutomaticStateManagement="True" x:Name="Ribbon">
        <ContentControl x:Name="SearchRibbon" Content="{Binding Path=SearchRibbon}" ContentTemplate="{DynamicResource SearchRibbonTemplate}" />
    </Ribbon:Ribbon>

Inside the contentcontrol ->

<DataTemplate x:Key="SearchRibbonTemplate">
    <ItemsControl ItemsSource="{Binding}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Ribbon:RibbonTabItem Header="Search">
                    <Ribbon:RibbonGroupBox Header="{Binding Path=DisplayName}" Width="100">
                        <Ribbon:Button Width="100" Icon="{Binding Path=TemplateResource}" LargeIcon="{Binding Path=TemplateResource}" Command="{Binding Path=Commands}" />
                    </Ribbon:RibbonGroupBox>
                </Ribbon:RibbonTabItem>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</DataTemplate>

Upvotes: 0

Views: 1198

Answers (1)

Charleh
Charleh

Reputation: 14002

Your Ribbon control expects an object of RibbonTabItem type (since it houses an

ObservableCollection<RibbonTabItem> 

so you can only add the RibbonTabItem class to it - you need to make sure your control allows other elements inside it. Some 3rd party controls get round this by providing a content control inside the inner item of their custom control (i.e. let RibbonTabItem have a ContentControl inside it) or allow you to customise the item template

You need to change your implementation of Ribbon or change the functionality of RibbonTabItem for this to work. Look at ItemsControl.Items property and see what type that is. You should try using that type for your ObservableCollection

Upvotes: 1

Related Questions