I am a newbie to WPF .As I have added a textbox column to my datagrid column header for search functionality. I am unable to access the textbox name which is inside my datagrid in my project .
My 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>
My 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;
I have written the textchange event in the code behind and I want to access the "NameTextBox" (name of the textbox) in my C#. Kindly help me out.
DataGridColumn.Header
takes any kind of content, even other controls. Instead of using a DataTemplate
, just put the TextBox
directly into the column's 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>
This will let you access the TextBox
by name. As a note: bindings would still work with the DataTemplate
approach, as long as you set RelativeSource
correctly.