Kellso Asked:2020-01-17 23:15:38 +0000 UTC2020-01-17 23:15:38 +0000 UTC 2020-01-17 23:15:38 +0000 UTC 从内部 TextBox/ComboBox 在 DataGrid 中搜索。Mvvm - C# WPF/.NET 4.x 772 今天是个好日子!我对如何在 DataGrid 中放置几个 TextBoxes 或 ComboBoxes 非常感兴趣,以便它们可以按行搜索或在 DataGrid 中找不到时查询数据库。 我不需要代码示例,我知道如何自己编写,但我不会拒绝 XAML 或指向可以阅读它的源的链接 PS我的意思是在屏幕截图中: c# 1 个回答 Voted Best Answer Сергей Игнахин 2020-01-17T23:22:24Z2020-01-17T23:22:24Z 为了在标题中放置一些东西,重新定义HeaderTemplate任何类型的列就足够了。 例子: <Window x:Class="GridHeader.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:GridHeader" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <DataGrid> <DataGrid.Columns> <DataGridTextColumn Width="150"> <DataGridTextColumn.HeaderTemplate> <DataTemplate> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <TextBlock Text="Заголовок 1" HorizontalAlignment="Center"/> <TextBox Grid.Row="1" Text="TextBox в заголовке" Margin="5"/> </Grid> </DataTemplate> </DataGridTextColumn.HeaderTemplate> </DataGridTextColumn> <DataGridTextColumn Width="*"> <DataGridTextColumn.HeaderTemplate> <DataTemplate> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <TextBlock Text="Заголовок 2" HorizontalAlignment="Center"/> <ComboBox Grid.Row="1" Text="ComboBox в заголовке" Margin="5"/> </Grid> </DataTemplate> </DataGridTextColumn.HeaderTemplate> </DataGridTextColumn> <DataGridTextColumn Width="200"> <DataGridTextColumn.HeaderTemplate> <DataTemplate> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <TextBlock Text="Заголовок 3" HorizontalAlignment="Center"/> <CheckBox Grid.Row="1" Content="CheckBox в заголовке" Margin="5"/> </Grid> </DataTemplate> </DataGridTextColumn.HeaderTemplate> </DataGridTextColumn> </DataGrid.Columns> </DataGrid> </Grid> </Window>
为了在标题中放置一些东西,重新定义
HeaderTemplate任何类型的列就足够了。例子: