I am learning on how to create custom control in WPF. I have few problems that I am stuck with.
Basically, I am trying to create custom control for navigation bar that has two level.
- Level 1 contains a big icons with a title text; and
- Level 2 contains a small icons where the user can click on it and event will be generated.
This is what I am trying to archieve:
--------------------------------
| |
| ICON TITLE 1 |
| |
| small icon option 1 |
| small icon option 2 |
| small icon option 3 |
| |
| |
| ICON TITLE 2 |
| |
| small icon option 1 |
| small icon option 2 |
| etc... |
| |
--------------------------------
Here is my Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Global.WPFs.GUIs">
<Style TargetType="{x:Type local:GNavBar}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:GNavBar}">
<ScrollViewer x:Name="PART_Scroll"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
VerticalScrollBarVisibility="Auto"
Focusable="False">
<ItemsControl x:Name="PART_Items">
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Margin" Value="0"/>
<Setter Property="Control.Padding" Value="0 8 0 2"/>
<Setter Property="Control.HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Stretch">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="76"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="76" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="{Binding ImgSrc}" Width="72" Height="72" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
<TextBlock Grid.Column="1" Text="{Binding Text}" Margin="4 0 0 0" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="20"></TextBlock>
</Grid>
<ListBox ItemsSource="{Binding Items}"
BorderThickness="0"
Background="Transparent"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Margin" Value="0"/>
<Setter Property="Control.Padding" Value="0"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Border>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="36"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="16" />
<ColumnDefinition Width="16" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="1" Source="{Binding ImgSrc}" Width="16" Height="16" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
<TextBlock Grid.Column="2" Text="{Binding Text}" Margin="4 0 0 0" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="16"></TextBlock>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Everything is working fine, but I have two problems:
- How do I go about with detecting which item is clicked so I can raise the event to the parent class?
- Scrolling works fine if I scroll in TITLE, but as soon as the mouse pointer hit listbox, the scrolling stops working.
Thanks...

ListBoxwill consume the scroll event, as it contains a list of items, so you need to handle the scroll event and set the e.Handled = false. For the events of being clicked then you need to attach an event handler to Image and TextBlock. That could be done through Style and an event setter.TreeView. Which would do everything you need to achieve.node(eg.item in the treeview) has been clicked (so that I can use it to raise event to calling function) inside a template? Also, is it possible to use TreeView to display different size of icons (eg. title icon is a big icon 64x64 where as small icon is 16x16)?