سؤال

I have a datagrid which I have replaced the headers with Comboboxes. I got this part working by using the below code in wpf, but my problem is I don't want to have the combobox for the first column header.

I have tried this way :

<DataGrid.ColumnHeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <EventSetter Event="Click" Handler="ColumnHeaderClick" />
                    <Style.Triggers>
                        <DataTrigger Value="True">
                            <DataTrigger.Binding>
                                <MultiBinding Converter="{StaticResource ColumnHeaderToComboBoxConverter}">
                                    <Binding Path="ColumnHeader"/>
                                    <Binding Path="DataContext.FirstColumnHeader" ElementName="control" />
                                </MultiBinding>
                            </DataTrigger.Binding>
                            <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>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.ColumnHeaderStyle>

In the Multibinding part, I want to send in 2 values to the converter :

As the first value I am trying to send the current column header :

<Binding Path="ColumnHeader"/>

And as the second value every time I send in a specific value which is the first column header:

<Binding Path="DataContext.FirstColumnHeader" ElementName="control" />

I want to compare these two in the converter if they are they same then return false otherwise it should return true (It means add the combo box to column header). But the does not send in any information to the converter which is named : ColumnHeaderToComboBoxConverter.

Any help will be appreciated.enter image description here

I have attached an Image also.

هل كانت مفيدة؟

المحلول

You can set the first column header style to null, so this style won't affect it :

Style:

 <DataGrid.ColumnHeaderStyle>
     <Style TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="ContentTemplate">
          <Setter.Value>
            <DataTemplate>
              <ComboBox ItemsSource="{Binding DataContext.ArticleAttributes, Source={x:Reference control}}"/>
            </DataTemplate>
           </Setter.Value>
         </Setter>
     </Style>
 </DataGrid.ColumnHeaderStyle>

Columns Definition:

    <DataGrid.Columns>
       <DataGridTextColumn Binding="{Binding ID}" Header="ID" HeaderStyle="{x:Null}"/>
       <DataGridTextColumn Binding="{Binding Name}" Header="Name" />
       <DataGridTextColumn Binding="{Binding Age}" Header="Age"/>
   </DataGrid.Columns>

AutoGenerateColumns

In case your columns are being generated by the dataGrid, you can add an event handler to AutoGeneratingColumn event, then set to null the column which would be the first column in the first call, and then remove the handler:

Add Handler:

dataGrid.AutoGeneratingColumn += DataGrid_AutoGeneratingColumn;

Handler:

   private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        e.Column.HeaderStyle = null;
        dataGrid.AutoGeneratingColumn -= DataGrid_AutoGeneratingColumn;
    }

نصائح أخرى

Have you checked DataTemplateSelector? You can choose a different DataTemplate...

check: http://msdn.microsoft.com/en-us/library/ms742521.aspx

Regards,

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top