- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一种情况,当它是除一个值之外的所有值之一时,我需要对 ComboBox 中的选定项目进行不同的样式设置(使文本加粗)。例如,在标有“您最喜欢的原色是什么?”的下拉框中。我有四个选择:No Preference
, Red
, Green
, 和 Blue
. ComboBox 项目只是具有默认样式的文本,没有图像或其他任何花哨的东西,并且是 C# 类,没有包装在 ComboBoxItems 中。
当用户从列表中指定首选项时,我想通过将折叠列表中所选项目的文本设置为粗体来突出显示该选择。如果用户选择No Preference
,字体粗细应该保持正常。
通过将 ComboBox 上的 FontWeight 属性设置为样式中的粗体,并将 DataTrigger 定义为 SelectedItem != No Preference
,我已经实现了 90% 的解决方案。 .但是,这会为 ComboBox 的项目列表中的所有项目设置样式,包括下拉列表中的所有项目。我希望这些项目始终以正常的字体粗细显示。
这可能吗?
编辑
我一直在尝试@crazyarabian 使用 MultiTrigger 设置 ComboBoxItem 样式的方法。样式定义为:
<Style x:Key="SelectedItemStyle">
<Setter Property="ComboBoxItem.FontWeight" Value="Normal" />
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="ComboBoxItem.IsSelected" Value="True" />
<Condition Binding="{Binding IsNoPreferenceSelected,Mode=OneWay}" Value="False" />
</MultiTrigger.Conditions>
<Setter Property="ComboBoxItem.FontWeight" Value="Bold" />
</MultiTrigger>
</Style.Triggers>
</Style>
<DataTemplate x:Key="PrimaryColoursTemplate" DataType="{x:Type ViewModels:PrimaryColoursViewModel}">
<ComboBox ItemsSource="{Binding PrimaryColours}" SelectedItem="{Binding SelectedPrimaryColour}"
ItemContainerStyle="{StaticResource SelectedItemStyle}" />
</DataTemplate>
System.Windows.Data Error: 8 : Cannot save value from target back to source. BindingExpression:Path=IsDropDownOpen; DataItem='ComboBox' (Name=''); target element is 'ToggleButton' (Name=''); target property is 'IsChecked' (type 'Nullable`1') InvalidOperationException:'System.InvalidOperationException: Must have non-null value for 'Property'.
NullReferenceException
而死,在上面的 InvalidOperationException 之后抛出(或者可能导致它,我无法破译输出)。我能想到的唯一可能导致这种情况的是在我的第二个 MultiTrigger 条件下解析绑定(bind)中的属性,但我根本没有收到任何绑定(bind)错误。这是堆栈跟踪的顶部,以防万一也有帮助:
InvalidOperationException:'System.InvalidOperationException: Must have non-null value for 'Property'.
at System.Windows.Condition.Seal(ValueLookupType type)
at System.Windows.ConditionCollection.Seal(ValueLookupType type)
at System.Windows.MultiTrigger.Seal()
at System.Windows.TriggerCollection.Seal()
at System.Windows.Style.Seal()
at System.Windows.StyleHelper.UpdateStyleCache(FrameworkElement fe, FrameworkContentElement fce, Style oldStyle, Style newStyle, Style& styleCache)
at System.Windows.FrameworkElement.OnStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
at System.Windows.Controls.ItemsControl.ApplyItemContainerStyle(DependencyObject container, Object item)
最佳答案
没有必要陷入像所有者绘制那样卑鄙的事情——我们在这里谈论的是 WPF,而不是 WinForms。在 WinForms 中,您唯一的解决方案是编写更多代码。在 WPF 中,我们可以通过一些非常简单的自定义模板来解决这个问题。对于这个例子,我使用了 Kaxaml ,一个免费的轻量级 XAML 编辑器。不需要代码隐藏。 卡克沙姆包含一堆称为简单样式的“入门”样式。我使用了 ComboBox 简单样式并从中进行了修改。因此,尽管这看起来像很多 XAML,但我实际上只是从样板文件开始并添加了几行代码。
您可能会想到更优雅的方式来触发字体粗细变化;我用了SelectedIndex
.
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<DataTemplate x:Key="SelectionBoxTextTemplate">
<TextBlock FontWeight="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}, Path=FontWeight}" Text="{Binding}"/>
</DataTemplate>
<ControlTemplate x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Border
x:Name="Border"
Grid.ColumnSpan="2"
Background="#C0C0C0"
BorderBrush="#404040"
BorderThickness="1"
CornerRadius="2"/>
<Border
Grid.Column="0"
Margin="1"
Background="#FFFFFF"
BorderBrush="#404040"
BorderThickness="0,0,1,0"
CornerRadius="2,0,0,2"/>
<Path
x:Name="Arrow"
Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M 0 0 L 4 4 L 8 0 Z"
Fill="#404040"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="ToggleButton.IsMouseOver" Value="true">
<Setter TargetName="Border" Property="Background" Value="#808080"/>
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="true">
<Setter TargetName="Border" Property="Background" Value="#E0E0E0"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border" Property="Background" Value="#EEEEEE"/>
<Setter TargetName="Border" Property="BorderBrush" Value="#AAAAAA"/>
<Setter Property="Foreground" Value="#888888"/>
<Setter TargetName="Arrow" Property="Fill" Value="#888888"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style x:Key="{x:Type ComboBox}" TargetType="{x:Type ComboBox}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="MinWidth" Value="120"/>
<Setter Property="MinHeight" Value="20"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid>
<ToggleButton
Name="ToggleButton"
Grid.Column="2"
ClickMode="Press"
Focusable="false"
IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
Template="{StaticResource ComboBoxToggleButton}">
</ToggleButton>
<ContentPresenter
Name="ContentSite"
HorizontalAlignment="Left"
Margin="3,3,23,3"
VerticalAlignment="Center"
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplate="{StaticResource SelectionBoxTextTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
IsHitTestVisible="False"/>
<TextBox
x:Name="PART_EditableTextBox"
HorizontalAlignment="Left"
Margin="3,3,23,3"
VerticalAlignment="Center"
Background="Transparent"
Focusable="False"
IsReadOnly="{TemplateBinding IsReadOnly}"
Style="{x:Null}"
Visibility="Hidden"/>
<Popup
Name="Popup"
AllowsTransparency="True"
Focusable="False"
IsOpen="{TemplateBinding IsDropDownOpen}"
Placement="Bottom"
PopupAnimation="Slide">
<Grid
Name="DropDown"
MaxHeight="{TemplateBinding MaxDropDownHeight}"
MinWidth="{TemplateBinding ActualWidth}"
SnapsToDevicePixels="True">
<Border
x:Name="DropDownBorder"
Background="#FFFFFF"
BorderBrush="#888888"
BorderThickness="1"/>
<ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained"/>
</ScrollViewer>
</Grid>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#888888"/>
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
<Trigger Property="Popup.AllowsTransparency" SourceName="Popup" Value="true">
<Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/>
<Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
</Trigger>
<Trigger Property="IsEditable" Value="true">
<Setter Property="IsTabStop" Value="false"/>
<Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/>
<Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
</Trigger>
<Trigger Property="SelectedIndex" Value="1">
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
<Trigger Property="SelectedIndex" Value="2">
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
<Trigger Property="SelectedIndex" Value="3">
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="{x:Type ComboBoxItem}" TargetType="{x:Type ComboBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Border Name="Border" Padding="2" SnapsToDevicePixels="true">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsHighlighted" Value="true">
<Setter TargetName="Border" Property="Background" Value="#DDDDDD"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#888888"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Margin="5" Text="What is your favorite primary colour?"/>
<ComboBox Width="150" SelectedIndex="0">
<ComboBoxItem>No Preference</ComboBoxItem>
<ComboBoxItem>Red</ComboBoxItem>
<ComboBoxItem>Green</ComboBoxItem>
<ComboBoxItem>Blue</ComboBoxItem>
</ComboBox>
</StackPanel>
</Page>
ContentTemplate
ContentPresenter
的属性(property)在
ComboBox
添加自定义数据模板 (
SelectionBoxTextTemplate
)。那
TextBlock
捕获它的
FontWeight
来自祖先组合框。然后我为各个项目添加了一个模板,强制它们使用正常的字体粗细。这得到了您正在寻找的结果:
关于wpf - 如何有条件地仅设置 ComboBox 所选项目中的文本样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6783063/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!