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
WPF Custom datagrid column header
提问by RayOldProf
I need to create a Custom dataGrid DataGridTextColumn like the sketch below:
我需要创建一个自定义 dataGrid DataGridTextColumn 如下图所示:
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
DataGridTemplateColumn
is 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 ItemsSource
it gives this look -
使用样品,ItemsSource
它看起来像 -
EDIT
编辑
In case you want to customize the header, you need to provide HeaderTemplate
for 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 -
这是外观 -