Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

c# - WPF Custom datagrid column header

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

Sketch

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 -

enter image description here

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 -

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...