WPF

DataGrid의 DataGridTemplateColumn안에서 Control (TextBlock)찾기

지오준 2022. 2. 24.
반응형

1. Xaml의 DataGrid의 디자인 소스코드

<DataGrid x:Name="dgManualData" HorizontalAlignment="Left" Height="Auto" VerticalAlignment="Top" Width="auto" AutoGenerateColumns="False" CanUserAddRows="False">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="DataType" Width="250">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock x:Name="tbAccountingType" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBlock>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

2. Cs파일의 소스코드

//DataGrid행 가져오기
var dataGridRow = dataOnDisplay.ItemContainerGenerator.ContainerFromIndex(dataOnDisplay.SelectedIndex) as DataGridRow;

//DataGrid열 가져오기
var dataGridCell = dataOnDisplay.Columns[0].GetCellContent(dataGridRow) as ContentPresenter;

//TextBlock가져오기
var tbAccountingType = (TextBlock)dataGridCell.ContentTemplate.FindName("tbAccountingType", dataGridCell);
반응형

댓글