I have a ComboBox
binding to an ObservableCollection<ElementType>
collection. I want to have unselectable separators where both Name and Type properties are set to null. When name is set to a string and Type is null, I want it to be an unselectable header/title. Otherwise, I want the elements to be selectable elements, but with a slight margin.
This is where I am so far:
My two problems are:
- The selected item is shown as ElementType object with full name space, rather than the Name string.
- Highlighting of the enabled elements are no longer shown on MouseOver.
XAML:
<ComboBox Grid.Column="1" Grid.Row="2" Style="{StaticResource ElementTypeComboBoxStyle}"
ItemsSource="{Binding Path=Element.ElementTypeList}"
SelectedItem="{Binding Path=Element.SelectedElementType}">
<ComboBox.Resources>
<converters:NullToBooleanConverter x:Key="NullToBooleanConverter" />
</ComboBox.Resources>
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=Name, Converter={StaticResource NullToBooleanConverter}}" Value="True" />
<Condition Binding="{Binding Path=Type, Converter={StaticResource NullToBooleanConverter}}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="IsEnabled" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Separator HorizontalAlignment="Stretch" />
</ControlTemplate>
</Setter.Value>
</Setter>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=Name, Converter={StaticResource NullToBooleanConverter}}" Value="False" />
<Condition Binding="{Binding Path=Type, Converter={StaticResource NullToBooleanConverter}}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="IsEnabled" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<TextBlock Text="{Binding Path=Name}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=Name, Converter={StaticResource NullToBooleanConverter}}" Value="False" />
<Condition Binding="{Binding Path=Type, Converter={StaticResource NullToBooleanConverter}}" Value="False" />
</MultiDataTrigger.Conditions>
<Setter Property="IsEnabled" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<TextBlock Text="{Binding Path=Name}" Margin="10,0,0,0" />
</ControlTemplate>
</Setter.Value>
</Setter>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
C#:
public class ElementType {
public ElementType(string name, Type type) {
Name = name;
Type = type;
}
public string Name { get; private set; }
public Type Type { get; private set; }
}
private static ObservableCollection<ElementType> _elementTypeList = new ObservableCollection<ElementType> {
// Controls
new ElementType("Controls:", null),
new ElementType("Analog Output Slider", typeof(AnalogOutputSliderControl)),
new ElementType("Digital Output Button", typeof(DigitalOutputButtonControl)),
new ElementType(null, null), // Separator
// Indicators
new ElementType("Indicators:", null),
new ElementType("Numeric Value", typeof(NumericValueIndicator)),
new ElementType("ROV Illustration", typeof(RovIllustration)),
new ElementType("Trend Graph", typeof(TrendGraphIndicator)),
new ElementType(null, null), // Separator
// Generic element
new ElementType("None", typeof(Element))
};
[XmlIgnore]
public static ObservableCollection<ElementType> ElementTypeList { get { return _elementTypeList; } }