# Custom Content

## ContentTemplate and CompactContentTemplate

Each RadOfficeNavigationBarItem supports setting complex structures of elements to its `ContentTemplate` and `CompactContentTemplate` properties. These structures will represent the values set to its `Content` and `CompactContent` properties.

> important The CompactContent, CompactContentTemplate, and CompactContentTemplateSelector properties will be used when the `IsInCompactMode` property is set to **True**. To learn more about this items display mode, check the [Display Modes](https://www.telerik.com/products/wpf/documentation/controls/radofficenavigationbar/display-modes) article.

**Example complex ContentTemplate**

```XAML
<telerik:RadOfficeNavigationBarItem Content="Mail">
    <telerik:RadOfficeNavigationBarItem.ContentTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Border Width="12" Height="12" Background="Red"/>
                <TextBlock Text="{Binding}" Foreground="Red" Margin="5 0 0 0"/>
            </StackPanel>
        </DataTemplate>
    </telerik:RadOfficeNavigationBarItem.ContentTemplate>
</telerik:RadOfficeNavigationBarItem>
```

> caption RadOfficeNavigationBarItem with a custom ContentTemplate

![WPF RadOfficeNavigationBarItem with a custom ContentTemplate](https://www.telerik.com/products/wpf/documentation/assets/536e7c7034f6ac9a9183211fe78ad783/officenavigationbar-custom-content-0.png)

**Example complex CompactContentTemplate**

```XAML
<telerik:RadOfficeNavigationBar IsInCompactMode="True">
    <telerik:RadOfficeNavigationBarItem CompactContent="&#xe809;">
        <telerik:RadOfficeNavigationBarItem.CompactContentTemplate>
            <DataTemplate>
                <telerik:RadGlyph Glyph="{Binding}" Foreground="Red"/>
            </DataTemplate>
        </telerik:RadOfficeNavigationBarItem.CompactContentTemplate>
    </telerik:RadOfficeNavigationBarItem>
</telerik:RadOfficeNavigationBar>
```

> caption RadOfficeNavigationBarItem with a custom CompactContentTemplate

![WPF RadOfficeNavigationBarItem with a custom CompactContentTemplate](https://www.telerik.com/products/wpf/documentation/assets/6552a1b6d7968d1e386f8a437d7861c8/officenavigationbar-custom-content-1.png)

## ContentTemplateSelector and CompactContentTemplateSelector

The ContentTemplateSelector and CompactContentTemplateSelector allow you to apply different data templates based on custom logic.

For the purpose of the following example, the `ContentTemplateSelector` property of the RadOfficeNavigationBarItem will be used.

> important The example utilizes the RadGlyph element. To learn more about it, check the [RadGlyph](https://www.telerik.com/products/wpf/documentation/styling-and-appearance/glyphs/common-styles-appearance-glyphs-radglyph) article.

Define a sample model class that will hold the name and the glyph as string of each item.

**Define a model**

```C#
public class ItemModel
{
    public string Name { get; set; }
    public string GlyphString { get; set; }
}
```

```VB.NET
Public Class ItemModel
    Public Property Name As String
    Public Property GlyphString As String
End Class
```

After that, create a view model that will contain a collection of ItemModels instances. This collection will be bound to the `ItemsSource` property of the `RadOfficeNavigationBar` control.

**Define a view model**

```C#
public class ItemsViewModel
{
    public ItemsViewModel()
    {
        this.ItemModels = new ObservableCollection<ItemModel>
        {
            new ItemModel() { Name = "Mail", GlyphString = "xe809" },
            new ItemModel() { Name = "Calendar", GlyphString = "No glyph" },
        };
    }

    public ObservableCollection<ItemModel> ItemModels { get; set; }
}
```

```VB.NET
Public Class ItemsViewModel
Public Sub New()
ItemModels = New ObservableCollection(Of ItemModel) From {
  New ItemModel() With {
      .Name = "Mail",
        .GlyphString = "xe809"
    },
    New ItemModel() With {
      .Name = "Calendar",
        .GlyphString = "No glyph"
    }
}
End Sub

Public Property ItemModels As ObservableCollection(Of ItemModel)
End Class
```

Next, we can define a custom `DataTemplateSelector` class and override its `SelectTemplate` method. In it, we will check if the GlyphString property equals **"No glyph"** and return different `DataTemplate` depending on this check.

**Define a custom DataTemplateSelector**

```C#
public class GlyphContentTemplateSelector : DataTemplateSelector
{
    public DataTemplate GlyphDataTemplate { get; set; }
    public DataTemplate DefaultDataTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        ItemModel itemModel = (ItemModel)item;

        if (itemModel.GlyphString == "No glyph")
        {
            return this.DefaultDataTemplate;
        }

        return this.GlyphDataTemplate;
    }
}
```

```VB.NET
Public Class GlyphContentTemplateSelector
    Inherits DataTemplateSelector
    Public Property GlyphDataTemplate As DataTemplate
    Public Property DefaultDataTemplate As DataTemplate

    Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As DependencyObject) As DataTemplate
        Dim itemModel As ItemModel = CType(item, ItemModel)

        If itemModel.GlyphString Is "No glyph" Then
            Return DefaultDataTemplate
        End If

        Return GlyphDataTemplate
    End Function
End Class
```

Create an instance of the custom DataTemplateSelector class in XAML and set it to each RadOfficeNavigationBarItem element.

**Instantiate the custom DataTemplateSelector in XAML**

```XAML
<Application.Resources>
    <telerik:StringToGlyphConverter x:Key="StringToGlyphConverter"/>

    <local:GlyphContentTemplateSelector x:Key="GlyphContentTemplateSelector">
        <local:GlyphContentTemplateSelector.DefaultDataTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <telerik:RadGlyph Glyph="{StaticResource GlyphCalendar}" Margin="0 0 3 0"/>
                    <TextBlock Text="{Binding Name}"/>
                </StackPanel>
            </DataTemplate>
        </local:GlyphContentTemplateSelector.DefaultDataTemplate>
        <local:GlyphContentTemplateSelector.GlyphDataTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <telerik:RadGlyph Glyph="{Binding GlyphString, Converter={StaticResource StringToGlyphConverter}}" Margin="0 0 3 0"/>
                    <TextBlock Text="{Binding Name}"/>
                </StackPanel>
            </DataTemplate>
        </local:GlyphContentTemplateSelector.GlyphDataTemplate>
    </local:GlyphContentTemplateSelector>

    <Style x:Key="OfficeNavigationBarItemStyle" TargetType="telerik:RadOfficeNavigationBarItem" BasedOn="{StaticResource RadOfficeNavigationBarItemStyle}">
        <Setter Property="ContentTemplateSelector" Value="{StaticResource GlyphContentTemplateSelector}"/>
    </Style>
</Application.Resources>
```

Finally, apply the new Style to the `ItemContainerStyle` property of the RadOfficeNavigationBar control.

**Set the custom Style to the ItemContainerStyle property**

```XAML
<Grid>
    <Grid.DataContext>
        <local:ItemsViewModel/>
    </Grid.DataContext>
    <telerik:RadOfficeNavigationBar ItemsSource="{Binding ItemModels}" ItemContainerStyle="{StaticResource OfficeNavigationBarItemStyle}"/>
</Grid>
```

> caption RadOfficeNavigationBarItems with custom ContentTemplateSelector

![WPF RadOfficeNavigationBarItems with custom ContentTemplateSelector](https://www.telerik.com/products/wpf/documentation/assets/d18867405b35813192f6e10eae10d081/officenavigationbar-custom-content-2.png)

## See Also

* [Drag and Drop](https://www.telerik.com/products/wpf/documentation/controls/radofficenavigationbar/drag-drop)
* [Display Modes](https://www.telerik.com/products/wpf/documentation/controls/radofficenavigationbar/display-modes)
* [Peek Popup](https://www.telerik.com/products/wpf/documentation/controls/radofficenavigationbar/peek-popup)
