简体   繁体   English

如何将组合框添加到Datagrid中除wpf中的第一列之外的所有ColumnHedares中?

[英]How to add Comboboxes to all ColumnHedares in Datagrid except for the first column in wpf?

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. 我通过在wpf中使用以下代码来使这一部分正常工作,但是我的问题是我不希望第一列标题具有组合框。

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 : Multibinding部分,我想向转换器发送2个值:

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). 我想在转换器中比较它们是否相同,然后返回false,否则返回true(这意味着将组合框添加到列标题)。 But the 但是 does not send in any information to the converter which is named : ColumnHeaderToComboBoxConverter. 不会将任何信息发送到名为ColumnHeaderToComboBoxConverter的转换器。

Any help will be appreciated. 任何帮助将不胜感激。 在此处输入图片说明

I have attached an Image also. 我还附了一张图片。

You can set the first column header style to null, so this style won't affect it : 您可以将第一列标题样式设置为null,因此该样式不会影响它:

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 的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: 如果您的列是由dataGrid生成的,则可以将事件处理程序添加到AutoGeneratingColumn事件中,然后将将作为第一次调用中第一列的列设置为null,然后删除该处理程序:

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? 您是否检查过DataTemplateSelector? You can choose a different DataTemplate... 您可以选择其他DataTemplate ...

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

Regards, 问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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