Search code examples
c#wpfcomboboxdatagridcolumnheader

How to bind the value of Selected Item in a combobox in DataGridHeader to one specific column not to all Datagrid columns in wpf?


enter image description here

I have a DataGrid which has been filled with a DataTable using Itemsources properties, it means that all columns and rows have been created automatically.I need to add a combobox to each column header cell in my grid.

I have done that part using this piece of code :

<DataGrid Grid.Row="1" Margin="0 5 0 0" ItemsSource="{Binding SelectedExcel}">
   <DataGrid.ColumnHeaderStyle>
         <Style TargetType="{x:Type DataGridColumnHeader}">
             <Setter Property="ContentTemplate">
                 <Setter.Value>
                     <DataTemplate Template="">
                         <ComboBox ItemsSource="{Binding DataContext.ArticleAttributes, Source={x:Reference control}}" SelectedItem="{Binding DataContext.SelectedArticleAttribute, Source={x:Reference control}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                     </DataTemplate>
                 </Setter.Value>
             </Setter>
         </Style>
   </DataGrid.ColumnHeaderStyle>
</DataGrid>

The problem is when I select one of the items in one of the comboboxes the value of all the comboboxes will be changed. I have attached an image. Any idea how should I fix that? And also I need to know the combobox of which column has been set. I need to use that column later. I am well aware of DataGrid SelectedItem and SelectedIndex properties, any other ideas?

I tried to fix it this way:

<DataGrid.ColumnHeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Style.Triggers>
                        <DataTrigger Value="True">
                            <DataTrigger.Binding>
                                <MultiBinding Converter="{StaticResource MultiEquilityConverter}">
                                    <Binding RelativeSource="{RelativeSource Self}" />
                                    <Binding Path="CurrentColumn" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}" />
                                </MultiBinding>
                            </DataTrigger.Binding>
                        </DataTrigger>
                    </Style.Triggers>
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding DataContext.ArticleAttributes, Source={x:Reference control}}" SelectedItem="{Binding DataContext.SelectedArticleAttribute, Source={x:Reference control}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.ColumnHeaderStyle>

But the MultiEquilityConverter never fires.

I have been searching internet for two days, but I did not find any way yet.


Solution

  • I have fixed it this way, for the combo boxes I have used this piece of code :

    <DataGrid.ColumnHeaderStyle>
                    <Style TargetType="{x:Type DataGridColumnHeader}">
                        <EventSetter Event="Click" Handler="ColumnHeaderClick" />
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <ComboBox ItemsSource="{Binding DataContext.ArticleAttributes, Source={x:Reference control}}">
                                        <l:Interaction.Triggers>
                                            <l:EventTrigger EventName="SelectionChanged">
                                                <l:InvokeCommandAction Command="{Binding DataContext.ArticleAttributeCommand, Source={x:Reference control}}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}, Path=SelectedItem}" />
                                            </l:EventTrigger>
                                        </l:Interaction.Triggers>
                                    </ComboBox>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </DataGrid.ColumnHeaderStyle>
    

    And for getting the column number this way :

    <EventSetter Event="Click" Handler="ColumnHeaderClick" />
    

    As it has been mentioned above.