简体   繁体   中英

WPF DataGridComboboxColumn same style as Combobox

I've got an an application which contains a datagrid. In this datagrid, there are columns of the type DataGridComboboxColumn. The trick is that I defined a style in a resource dictionary which targets Combobox, but it seems it does not apply to the DataGridComboboxColumn in editing mode, but it does on a "regular" Combobox.

I can't repeat the resource as a DataGridComboboxColumn is not available as a target type.

Any idea ?

<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>

You can assign the EditingElementStyle to combobox style which you created. refer below code.

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM