WPF DataGridComboboxColumn与Combobox的风格相同

时间:2015-02-09 21:30:10

标签: c# wpf datagrid

我有一个包含数据网格的应用程序。在此数据网格中,存在DataGridComboboxColumn类型的列。诀窍在于我在资源字典中定义了一个以Combobox为目标的样式,但它似乎不适用于编辑模式下的DataGridComboboxColumn,但它适用于“常规”Combobox。

我无法重复该资源,因为DataGridComboboxColumn不可用作目标类型。

有什么想法吗?

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Resources.xaml"/>
    </ResourceDictionary.MergedDictionaries>
<Style x:Key="StandardComboBox" TargetType="ComboBox">
        <Setter Property="Foreground" Value="{StaticResource Foreground}"/>
        <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>
        <Style.Resources>
            <Style TargetType="ComboBoxItem">
                <Setter Property="SnapsToDevicePixels" Value="true"/>
                <Setter Property="OverridesDefaultStyle" Value="true"/>
                <Setter Property="Template">
                ...
                </Setter>
            </Style>
        </Style.Resources>
    </Style>
</ResourceDictionary>


<Window x:Class="OtdrQualifTools.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Stretch" ItemsSource="{Binding AcquisitionList}"
                              Margin="0,200,0,0" Name="dataGridAcquisitions" VerticalAlignment="Top" >
            <DataGrid.Columns>
                <DataGridComboBoxColumn Header="Mode" Width="SizeToHeader" 
                                                    SelectedItemBinding="{Binding AcquisitionMode}" 
                                                    ItemsSource="{Binding Source={StaticResource AcquisitionModeValues}}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:3)

您可以将EditingElementStyle指定为您创建的组合框样式。参考下面的代码。

<DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Stretch" ItemsSource="{Binding AcquisitionList}"
                          Margin="0,200,0,0" Name="dataGridAcquisitions" VerticalAlignment="Top" >
            <DataGrid.Columns>
                <DataGridComboBoxColumn Header="Mode" Width="SizeToHeader" 
                                        SelectedItemBinding="{Binding AcquisitionMode}" 
                                        EditingElementStyle="{StaticResource StandardComboBox}"
                                        ItemsSource="{Binding Source={StaticResource AcquisitionModeValues}}"/>
            </DataGrid.Columns>
        </DataGrid>