如何在 WPF DataGrid 中设置组合框的样式?
我有一个包含一些 DataGridComboBoxColumns 的 DataGrid。
我希望这些列内的组合框应用某种样式。
所以我尝试将以下内容添加到我的 DataGrid 资源中:
<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="Background" Value="{StaticResource DefaultBackgroundBrush}"/>
<Setter Property="BorderBrush" Value="{StaticResource DefaultBorderBrush}"/>
<Setter Property="Foreground" Value="{StaticResource DefaultForegroundBrush}"/>
<Setter Property="FontFamily" Value="{StaticResource DefaultFontFamily}"/>
<Setter Property="FontSize" Value="{StaticResource DefaultFontSize}"/>
<Setter Property="Padding" Value="2"/>
</Style>
根本不执行任何操作。
令人惊讶的是,如果我添加:
<Style x:Key="{x:Type ComboBoxItem}" TargetType="ComboBoxItem">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Foreground" Value="Red"/>
</Style>
同一组合框中的组合框项目将正确获得红色前景色。
所以基本上,我无法在 dataGrid 中设置组合框的样式,但我可以在这些相同的组合框中设置组合框项的样式。
非常令人惊讶...
有人知道这里发生了什么以及我如何解决我的问题吗?
谢谢
I have a DataGrid containing some DataGridComboBoxColumns.
I would like the comboBoxes inside those columns to have a certain style applied.
so I tried adding the following to my DataGrid's Resources:
<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="Background" Value="{StaticResource DefaultBackgroundBrush}"/>
<Setter Property="BorderBrush" Value="{StaticResource DefaultBorderBrush}"/>
<Setter Property="Foreground" Value="{StaticResource DefaultForegroundBrush}"/>
<Setter Property="FontFamily" Value="{StaticResource DefaultFontFamily}"/>
<Setter Property="FontSize" Value="{StaticResource DefaultFontSize}"/>
<Setter Property="Padding" Value="2"/>
</Style>
does not do anything at all.
amazingly enough, if I add:
<Style x:Key="{x:Type ComboBoxItem}" TargetType="ComboBoxItem">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Foreground" Value="Red"/>
</Style>
the comboBoxItems inside the same comboBoxes will get the red foreground color Correctly.
so basically, I cannot set the style of the comboBoxes inside my dataGrid, but I can set the style of the comboBoxItems inside those same comboBoxes.
quite surprising...
does anyone have a clue about what's going on here and how I can solve my problem?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这究竟是做什么的?
x:Key="{x:Type ComboBox}"
如果您希望将样式应用于子树中的所有控件,则不应分配键。
编辑:要将样式应用到 ComboBox,您必须调整属性
DataGridComboBoxCotlumn.ElementStyle
和DataGridComboBoxColumn.EditingElementStyle
。What exactly is this supposed to do?
x:Key="{x:Type ComboBox}"
You should not assign keys if you want styles to be applied to all controls in the subtree.
Edit: To apply the styles to the ComboBoxes you must adjust the properties
DataGridComboBoxCotlumn.ElementStyle
andDataGridComboBoxColumn.EditingElementStyle
.