简体   繁体   中英

WPF Custom datagrid column header

I need to create a Custom dataGrid DataGridTextColumn like the sketch below:

草图

The Red rectangles are TextBox and are used to search within the column.

so far i have implemented a datagrid like this (simplify Version):

        <DataGrid x:Name="CompassLogDataGrid"
              Grid.Row="1"
              Style="{DynamicResource ResourceKey=DataGridStyle}"
              IsTextSearchEnabled="True">

            <DataGrid.Columns>
                <DataGridTextColumn CellStyle="{StaticResource IdCell}"
                                x:Name="ID"
                                Header="ID"
                                Foreground="Black"
                                Binding="{Binding ID}"
                                DisplayIndex="0" />

                <DataGridTextColumn x:Name="DateGTC"
                                Header="Date"
                                Binding="{Binding DateString}"
                                CellStyle="{StaticResource DateGTCCell}" />
            </DataGrid.Columns

    </DataGrid

I have no idea how to create those textBoxes. Any clue would be appreciate it

DataGridTemplateColumn is what you are looking for. You can customize the template as per your need -

 <DataGrid>
       <DataGrid.Columns>
           <DataGridTemplateColumn>
               <DataGridTemplateColumn.CellTemplate>
                   <DataTemplate>
                      <TextBox BorderBrush="Red" BorderThickness="3" Margin="5"/>
                   </DataTemplate>
               </DataGridTemplateColumn.CellTemplate>
           </DataGridTemplateColumn>
       </DataGrid.Columns>
    </DataGrid>

With sample ItemsSource it gives this look -

在此处输入图片说明

EDIT

In case you want to customize the header, you need to provide HeaderTemplate for your column like this -

   <DataGrid>
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}"
                                Header="{Binding HeaderName}">
                <DataGridTextColumn.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Content, RelativeSource=
                                         {RelativeSource Mode=TemplatedParent}}"
                                       Margin="5"/>
                            <TextBox BorderBrush="Red" BorderThickness="3"
                                     Width="50" Margin="5"/>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTextColumn.HeaderTemplate>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

Here's the look -

在此处输入图片说明

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