如何将我的组合框样式设置为 Office 2016 Telerik 版本?
How to style my combobox as the Office 2016 Telerik version?
我有这个组合框 xaml 代码:
<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.
这就是我想要实现的:
我设法让项目看起来像图片中的那样,但我不知道如何对组合框本身进行操作。
如何在鼠标悬停时设置该颜色并在鼠标不在组合框上时将其恢复?
还有我如何实现右侧的三角手柄?
您收到错误是因为您将 Style
添加到 ComboBox
的 Items
属性。
您应该通过添加 <ComboBox.Style
元素来设置 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>
我有这个组合框 xaml 代码:
<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.
这就是我想要实现的:
我设法让项目看起来像图片中的那样,但我不知道如何对组合框本身进行操作。 如何在鼠标悬停时设置该颜色并在鼠标不在组合框上时将其恢复?
还有我如何实现右侧的三角手柄?
您收到错误是因为您将 Style
添加到 ComboBox
的 Items
属性。
您应该通过添加 <ComboBox.Style
元素来设置 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>