如何在wpf中访问datagrid列标题内的文本框?

如何在wpf中访问datagrid列标题内的文本框?,wpf,filter,datagrid,controls,datagridcolumnheader,Wpf,Filter,Datagrid,Controls,Datagridcolumnheader,我是WPF的新手。因为我已经在我的datagrid列标题中添加了一个文本框列,用于搜索功能。我无法访问项目中datagrid中的文本框名称 我的XAML: <DataGrid DataContext="{Binding Details}" Name="g1" ItemsSource="{Binding}" > <DataGrid.Columns> <DataGridTextColumn Binding

我是WPF的新手。因为我已经在我的datagrid列标题中添加了一个文本框列,用于搜索功能。我无法访问项目中datagrid中的文本框名称

我的XAML:

<DataGrid  DataContext="{Binding Details}" Name="g1" ItemsSource="{Binding}"   >   
            <DataGrid.Columns>
               <DataGridTextColumn Binding="{Binding BrokerCode , UpdateSourceTrigger=PropertyChanged}"  HeaderStyle="{StaticResource CenterGridHeaderStyle}"  Header="Broker Code" x:Name="BrokerCode"  Width="100"  >
               <DataGridTextColumn.HeaderTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Vertical">
                                <Label VerticalAlignment="Center" HorizontalContentAlignment="Center" HorizontalAlignment="Center" Content="Broker Code" />
                                <TextBox x:Name="NameTextBox"  MinWidth="100"  TextChanged="SearchTextBox_TextChanged" Text="{Binding Filter, ElementName=mainwindow, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay }"/>    
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTextColumn.HeaderTemplate>
                </DataGridTextColumn>
</Datagrid>
我的C:

  SqlConnection con = new SqlConnection();
            con.ConnectionString = 
              ConfigurationManager.ConnectionStrings["connEmployee"].ConnectionString;
               con.Open();           
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "select BrokerCode from [SMAFI]";
                cmd.Connection = con;
                SqlDataAdapter adapter1 = new SqlDataAdapter(cmd);
                DataTable dt1 = new DataTable("SMAFI");
                var getdata = adapter1.Fill(dt1);
                List<Details> details = new List<Details>();
                details = (from DataRow row in dt1.Rows
                           select new Details()
                           {
                               BrokerCode = row["BrokerCode"].ToString()                                   
                           }).ToList();
                 g1.ItemsSource = details;
                  details = details.Where(x => 
                 x.BrokerCode.ToLower().StartsWith(NameTextBox.Text.ToLower())).ToList();
                 g1.ItemsSource = details;
我已经在代码隐藏中编写了textchange事件,我想访问C中textbox的名称textbox。请帮助我。

接受任何类型的内容,甚至其他控件。不使用DataTemplate,只需将文本框直接放入列的标题中:

<DataGrid DataContext="{Binding Details}" Name="g1" ItemsSource="{Binding}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding BrokerCode , UpdateSourceTrigger=PropertyChanged}" HeaderStyle="{StaticResource CenterGridHeaderStyle}" x:Name="BrokerCode" Width="100">
            <DataGridTextColumn.Header>
                <StackPanel Orientation="Vertical">
                    <Label VerticalAlignment="Center" HorizontalContentAlignment="Center" HorizontalAlignment="Center" Content="Broker Code" />
                    <TextBox x:Name="NameTextBox"  MinWidth="100" TextChanged="SearchTextBox_TextChanged" Text="{Binding Filter, ElementName=mainwindow, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                </StackPanel>
            </DataGridTextColumn.Header>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

这将允许您按名称访问文本框。需要注意的是:只要您正确设置相对资源,绑定仍然可以使用DataTemplate方法。

非常感谢。工作正常。非常感谢@anjalinsneha很高兴听到:那就请吧。