如何访问 wpf 中数据网格列 header 内的文本框?

How to access the textbox inside a datagrid column header in wpf?

我是 WPF 的新手。因为我已将文本框列添加到我的数据网格列 header 以实现搜索功能。我无法访问项目中数据网格内的文本框名称。

我的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# 中访问 "NameTextBox"(文本框的名称)。请帮助我。

DataGridColumn.Header 接受任何类型的内容,甚至其他控件。不要使用 DataTemplate,只需将 TextBox 直接放入列的 header:

<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>

这样您就可以按名称访问 TextBox。请注意:绑定仍然适用于 DataTemplate 方法,只要您正确设置 RelativeSource