C# WPF 自定义数据网格列标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15175546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:21:10  来源:igfitidea点击:

WPF Custom datagrid column header

c#wpfdatagrid

提问by RayOldProf

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

我需要创建一个自定义 dataGrid DataGridTextColumn 如下图所示:

Sketch

草图

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

红色矩形是 TextBox,用于在列内搜索。

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

我不知道如何创建这些文本框。任何线索将不胜感激

采纳答案by Rohit Vats

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

DataGridTemplateColumn就是你要找的。您可以根据需要自定义模板 -

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

With sample ItemsSourceit gives this look -

使用样品,ItemsSource它看起来像 -

enter image description here

在此处输入图片说明

EDIT

编辑

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

如果您想自定义标题,则需要HeaderTemplate像这样为您的列提供-

   <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

在此处输入图片说明