Как оформить мой комбинированный список как версию Telerik для Office 2016? - PullRequest
0 голосов
/ 03 мая 2020

У меня есть это поле со списком xaml code:

<ComboBox 
        Width="250" 
        Height="25"         
        Foreground="#545454"
        ItemsSource="{StaticResource ParametersArray}">


        <Style TargetType="{x:Type ComboBox}">
            <Setter Property="Template">
                <Setter.Value>

                    <ControlTemplate TargetType="{x:Type ComboBox}">
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">

                                <Setter 
                                        Property="Background" 
                                        Value="Blue" />

                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="false">

                                <Setter 
                                        Property="Background" 
                                        Value="Red" />

                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <ComboBox.ItemContainerStyle>

            <Style TargetType="{x:Type ComboBoxItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ComboBoxItem}">

                            <Border x:Name="Bd"
                                SnapsToDevicePixels="true"
                                Background="{TemplateBinding Background}"
                                Padding="{TemplateBinding Padding}">

                                <ContentPresenter 
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                            </Border>

                            <ControlTemplate.Triggers>
                                <Trigger Property="IsHighlighted" Value="true">

                                    <Setter 
                                        Property="Background" 
                                        Value="#C5C5C5" />

                                </Trigger>

                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ComboBox.ItemContainerStyle>

    </ComboBox>

Это приложение ломается с ошибкой:

System.Windows.Markup.XamlParseException
  HResult=0x80131501
  Message='Add value to collection of type 'System.Windows.Controls.ItemCollection' threw an exception.' Line number '40' and line position '19'.
  Source=PresentationFramework
  StackTrace:
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at WPFBlendTestProject.MainWindow.InitializeComponent() in C:\Users\username\Documents\Visual Studio 2019\Projects\WPFBlendTestProject\WPFBlendTestProject\MainWindow.xaml:line 1

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
InvalidOperationException: Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.

Вот чего я хочу достичь:

enter image description here

Мне удалось заставить элементы выглядеть так, как на картинке, но я не знаю, как это сделать с самим полем со списком. Как установить этот цвет при наведении курсора мыши и вернуть его, если мышь не находится над полем со списком?

Кроме того, как мне добиться этой трехугольной ручки справа?

1 Ответ

1 голос
/ 04 мая 2020

Вы получаете сообщение об ошибке, потому что добавляете Style к свойству Items ComboBox.

Вы должны установить для него свойство Style, добавив элемент <ComboBox.Style:

<ComboBox 
        Width="250" 
        Height="25"         
        Foreground="#545454"
        ItemsSource="{StaticResource ParametersArray}">
    <ComboBox.Style>
        <Style TargetType="{x:Type ComboBox}">
            <Setter Property="Template">
                ...
            </Setter>
        </Style>
    </ComboBox.Style>
    <ComboBox.ItemContainerStyle>
        <Style TargetType="{x:Type ComboBoxItem}">
            ...
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...