Ok, I will answer it. The thing was instead of ControlTemplate we should modify the ListBox's ItemTemplate and apply the same ControlTemplate as a DataTemplate.
<ListBox x:Name="_listBox"
SelectedItem="{Binding ElementName=SpecialToggleControl, Path=SelectedItem}"
SelectionMode="Single"
ItemsSource="{Binding ElementName=SpecialToggleControl, Path=ItemsSource}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="{Binding ElementName=SpecialToggleControl, Path=ColumnCount}" Background="Beige"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<ToggleButton Content="{TemplateBinding ContentControl.Content}"
IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type ListBoxItem}},Path=IsSelected}"
Name="toggleButton"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
MainWindow.xaml (where I used this Control): (The converter "cc" will now conditionally apply the colour)
<WpfApplication2:MutuallyExclusiveToggleControl.Resources>
<Style TargetType="{x:Type ToggleButton}">
<Style.Setters>
<Setter Property="Background" Value="{Binding Converter={StaticResource cc}}"></Setter>
</Style.Setters>
</Style>
</WpfApplication2:MutuallyExclusiveToggleControl.Resources>
Converter Code:
public class ColourConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if((string)value == "Buy")
{
return Brushes.Blue;
}
if ((string)value == "Sell")
{
return Brushes.Red;
}
return Brushes.White;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}