ComboBox元素的样式

时间:2017-11-06 09:57:58

标签: c# wpf

我对ComboBox集合有ObservableCollection<ElementType>绑定。我想要具有不可选择的分隔符,其中Name和Type属性都设置为null。当name设置为字符串且Type为null时,我希望它是一个不可选择的标题/标题。否则,我希望元素是可选择的元素,但略有余量。

这是我到目前为止的地方:

enter image description here

我的两个问题是:

  • 所选项目显示为具有全名空间的ElementType对象,而不是名称字符串。
  • 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; } }

1 个答案:

答案 0 :(得分:0)

我选择不去寻找模板选择器,因为我认为它对我的需求来说有点沉重。我在XAML中找到了解决问题的方法,给了我以下结果。它也适用于箭头键,因为禁用了分隔符和标题。只有&#34;问题&#34;是标题的灰色,但是我现在就把它留下来了。

enter image description here

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.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Name}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
    <ComboBox.ItemContainerStyle>
        <Style TargetType="{x:Type ComboBoxItem}">
            <Setter Property="IsEnabled" Value="False" />
            <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="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="False" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="IsEnabled" Value="True" />
                    <Setter Property="Padding" Value="10,0,0,0" />
                </MultiDataTrigger>                            
            </Style.Triggers>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>