我在 DataGridColumnHeadertemplate 中放置了一个 textBox(txtSearch),如下所示:
<UserControl.Resources>
<Style x:Key="DataGridColumnHeaderStyle1" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Grid VerticalAlignment="Center" HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{TemplateBinding Content}" HorizontalAlignment="Center" />
<Grid Grid.Row="1" >
<TextBox x:Name="txtSearch" Tag="{Binding DataGridTextColumn.Name, ElementName=grd}" Text="" HorizontalAlignment="Stretch" BorderThickness="1" TextChanged="TextBox_TextChanged" />
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
并将其用于这样的数据网格:
<DataGrid x:Name="grd" ItemsSource="{Binding Source={StaticResource theSource}}" AutoGenerateColumns="False" ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle1}">
<DataGrid.Columns >
<DataGridTextColumn Header="نام" Binding="{Binding Title}" x:Name="Title" ></DataGridTextColumn>
<DataGridTextColumn Header="ID" Binding="{Binding ParentID}" x:Name="ParentID"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
现在我需要将 txtSearch Tag 属性绑定到 DataGridTextColumn.Name。我该怎么做?