Home > DeveloperSection > Forums > how can i access the textbox inside the datagrid using the events of that datagrid?
Jayden Bell
Jayden Bell

Total Post:105

Points:739
Posted on    September-03-2013 8:20 AM

 WPF WPF 
Ratings:


 1 Reply(s)
 1857  View(s)
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>



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    September-03-2013 8:25 AM

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;


Don't want to miss updates? Please click the below button!

Follow MindStick