如何在WPF DataGrid中设置comboBox的风格? [英] how to set a comboBox's style inside a WPF DataGrid?

查看:706
本文介绍了如何在WPF DataGrid中设置comboBox的风格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGrid包含一些DataGridComboBoxColumns。



我希望这些列中的组合框具有一定的应用风格。



所以我尝试将以下内容添加到我的DataGrid的资源:

 < Style x:Key ={ x:Type ComboBox}TargetType ={x:Type ComboBox}> 
< Setter Property =SnapsToDevicePixelsValue =true/>
< Setter Property =OverridesDefaultStyleValue =true/>
< Setter Property =ScrollViewer.Horizo​​ntalScrollBarVisibilityValue =Auto/>
< Setter Property =ScrollViewer.VerticalScrollBarVisibilityValue =Auto/>
< Setter Property =ScrollViewer.CanContentScrollValue =true/>

< Setter Property =BackgroundValue ={StaticResource DefaultBackgroundBrush}/>
< Setter Property =BorderBrushValue ={StaticResource DefaultBorderBrush}/>
< Setter Property =ForegroundValue ={StaticResource DefaultForegroundBrush}/>
< Setter Property =FontFamilyValue ={StaticResource DefaultFontFamily}/>
< Setter Property =FontSizeValue ={StaticResource DefaultFontSize}/>
< Setter Property =PaddingValue =2/>
< / Style>

根本不做任何事。



令人惊讶的是,如果我添加:

 < Style x:Key ={x:Type ComboBoxItem}TargetType = ComboBoxItem> 
< Setter Property =SnapsToDevicePixelsValue =true/>
< Setter Property =OverridesDefaultStyleValue =true/>
< Setter Property =ForegroundValue =Red/>
< / Style>

同一组合框内的comboBoxItem将正确获取红色前景色。



所以基本上,我无法设置我的dataGrid中的comboBoxes的样式,但是我可以在同一个comboBox中设置comboBoxItem的样式。



相当惊讶...



有没有人知道这里发生了什么,以及我如何解决我的问题?



谢谢

解决方案

究竟该做什么? x:Key ={x:Type ComboBox}



如果要将样式应用于子树中的所有控件。



编辑:要将样式应用于组合框,您必须调整属性 DataGridComboBoxCotlumn.ElementStyle DataGridComboBoxColumn.EditingElementStyle


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

解决方案

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 and DataGridComboBoxColumn.EditingElementStyle.

这篇关于如何在WPF DataGrid中设置comboBox的风格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆