HOW CAN I ACCESS THE TEXTBOX INSIDE THE DATAGRID USING THE EVENTS OF THAT DATAGRID?

Jayden Bell

Total Post:110

Points:774
Posted by  Jayden Bell
WPF 
WPF
 3091  View(s)
Ratings:
Rate this:

I have a textbox in a datagrid that is designed using xaml. Can I access the textbox which has been designed in xaml previously in codefile using the events of the datagrid. Please help me.....................

      <Window x:Class="GridTextBox.Test"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowState="Maximized"

    Title="Test" Height="300" Width="300" Loaded="Window_Loaded">

<Grid>

    <Grid.RowDefinitions>

        <RowDefinition Height="30"/>

        <RowDefinition Height="*"/>

        <RowDefinition Height="30"/>

    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>

        <ColumnDefinition Width=".25*"/>

        <ColumnDefinition Width=".25*"/>

        <ColumnDefinition Width=".25*"/>

        <ColumnDefinition Width=".25*"/>

    </Grid.ColumnDefinitions>

    <DataGrid Grid.Row="1" Grid.Column="1"  Name="datagrid1" SelectionChanged="datagrid1_SelectionChanged" LoadingRowDetails="DataGrid_LoadingRowDetails"  Height="auto" Width="auto">

        <DataGrid.Columns>

            <DataGridTemplateColumn>

                <DataGridTemplateColumn.CellTemplate>

                    <DataTemplate>

                        <TextBox Name="txtEmpid" Text="hiiiiii"></TextBox>

                    </DataTemplate>

                </DataGridTemplateColumn.CellTemplate>

            </DataGridTemplateColumn>

        </DataGrid.Columns>

    </DataGrid>

</Grid>

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: how can i access the textbox inside the datagrid using the events of that datagrid?

    Hi Jayden,


    Firstly, you need to get use ItemContainerGenerator to get the correct row from data (in your datagrid1_SelectionChanged event).

    var row = (DataGridRow)datagrid1.ItemContainerGenerator.

                         ContainerFromItem(datagrid1.SelectedItem);

    then get the TextBlock like this:

    var text = datagrid1.Columns[0].GetCellContent(row) as TextBlock;

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!