How to get the cell content of DatagridTemplateColumn in wpf datagrid?

kedarK

I have a DataGridTemplateColumn whose header basically consists of StackPanel which contains a Button and TextBlock. I would want to access the Cell content or DataGridCell when the datagridrow is loaded.

My one of the datagridcolumn looks like this:

<DataGridTemplateColumn>
                <DataGridTemplateColumn.HeaderTemplate >
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"  Click="HeaderButtonClick">
                                <Button.Content>
                                    <Path Stretch="Fill" Fill="Black" Stroke="{x:Null}" StrokeThickness="0.5" 
                                          Data="M3.875,0 L5.125,0 5.125,3.875 9,3.875 9,5.125 5.125,5.125 5.125,9 3.875,9 3.875,5.125 0,5.125 0,3.875 3.875,3.875 3.875,0 z" />
                                </Button.Content>
                            </Button>
                            <TextBlock Text="Fund" Margin="20,0,0,0"/>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.HeaderTemplate>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
        <TextBlock Text="{Binding Fund}" Margin="20,0,0,0"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>

and the datagrid_loadingrow event looks like

    var dg = sender as DataGrid;
    if (dg != null)
    {
        foreach (var item in dg.Columns)
        {
            DataGridCell cell1 = item.GetCellContent(row) as DataGridCell;
         }
    }

Can anyone help me with this?

Sheridan

This is WPF, not WindowsForms. When using WPF, we data bind to UI control properties, rather than attempting to programmatically set these values. When data binding, you already have access to the data in your code behind/view model, so there really is no need to attempt to access it from the UI controls.

If you look at the DataGridTemplateColumn Class page on MSDN, you'll see this example:

<Grid>
    <Grid.Resources>
        <!--DataTemplate for Published Date column defined in Grid.Resources.  PublishDate is a property on the ItemsSource of type DateTime -->
        <DataTemplate x:Key="DateTemplate" >
            <StackPanel Width="20" Height="30">
                <Border Background="LightBlue" BorderBrush="Black" BorderThickness="1">
                    <TextBlock Text="{Binding PublishDate, StringFormat={}{0:MMM}}" FontSize="8" HorizontalAlignment="Center" />
                </Border>
                <Border Background="White" BorderBrush="Black" BorderThickness="1">
                    <TextBlock Text="{Binding PublishDate, StringFormat={}{0:yyyy}}" FontSize="8" FontWeight="Bold" HorizontalAlignment="Center" />
                </Border>
            </StackPanel>
        </DataTemplate>
        <!--DataTemplate for the Published Date column when in edit mode. -->
        <DataTemplate x:Key="EditingDateTemplate">
            <DatePicker SelectedDate="{Binding PublishDate}"  />
        </DataTemplate>
    </Grid.Resources>
    <DataGrid Name="DG1" ItemsSource="{Binding}" AutoGenerateColumns="False" >
        <DataGrid.Columns>
            <!--Custom column that shows the published date-->
            <DataGridTemplateColumn Header="Publish Date" CellTemplate="{StaticResource DateTemplate}" CellEditingTemplate="{StaticResource EditingDateTemplate}" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>

Notice the Bindings... for example, we can see this:

<DatePicker SelectedDate="{Binding PublishDate}"  />

This value can be accessed directly from the PublishDate property of the data bound object, rather than from the DataGrid:

DateTime publishDate = DataBoundCollection.ElementAt(relevantRowIndex).PublishDate;

You can find out more about data binding from the Data Binding Overview page on MSDN.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

WPF Datagrid Get Selected Cell Value

From Dev

WPF DataGrid and DataGridTemplateColumn Header Problems

From Dev

how to add multiple images in a cell of WPF Datagrid?

From Dev

WPF DataGrid - binding together controls of different DataGridTemplateColumn

From Dev

WPF DataGrid: How do I access a ComboBox in a specific row of a DataGridTemplateColumn?

From Dev

how to get row header value in wpf datagrid for the selected cell?

From Dev

update cell in datagrid wpf

From Dev

How i can get the value of cell in datagrid in wpf?

From Dev

wpf Datagrid Cell Formatting

From Dev

WPF - get row index for Combobox control in DataGridTemplateColumn

From Dev

How to skip WPF DataGrid cell focus and go to control inside it?

From Dev

In a WPF datagrid, how do I get the new row after cell edit?

From Dev

WPF DataGrid Trigger on cell content

From Dev

How do I stop row selection when a button in DataGridTemplateColumn is clicked in wpf DataGrid

From Dev

WPF datagrid, autocompletebox cell

From Dev

WPF : Get index of clicked / selected cell on DataGrid

From Dev

WPF Datagrid Get Selected Cell Value

From Dev

Get Text of TextBox that resides inside DataGridTemplateColumn in DataGrid

From Dev

How to modify cell after WPF DataGrid is bound

From Dev

Wpf Centering Cell content in DataGridTemplateColumn

From Dev

wpf Datagrid Cell Formatting

From Dev

WPF - get row index for Combobox control in DataGridTemplateColumn

From Dev

How to skip WPF DataGrid cell focus and go to control inside it?

From Dev

WPF, datagrid cell prefixing

From Dev

How to edit the cell type in a DataGrid Control wpf?

From Dev

How to set background for single cell of datagrid wpf not single row?

From Dev

How to get all the rows from DataGrid in WPF

From Dev

Datagrid Cell content is not visible in WPF

From Dev

How to unselect selected cell in WPF DataGrid with SelectionUnit = Cell

Related Related

  1. 1

    WPF Datagrid Get Selected Cell Value

  2. 2

    WPF DataGrid and DataGridTemplateColumn Header Problems

  3. 3

    how to add multiple images in a cell of WPF Datagrid?

  4. 4

    WPF DataGrid - binding together controls of different DataGridTemplateColumn

  5. 5

    WPF DataGrid: How do I access a ComboBox in a specific row of a DataGridTemplateColumn?

  6. 6

    how to get row header value in wpf datagrid for the selected cell?

  7. 7

    update cell in datagrid wpf

  8. 8

    How i can get the value of cell in datagrid in wpf?

  9. 9

    wpf Datagrid Cell Formatting

  10. 10

    WPF - get row index for Combobox control in DataGridTemplateColumn

  11. 11

    How to skip WPF DataGrid cell focus and go to control inside it?

  12. 12

    In a WPF datagrid, how do I get the new row after cell edit?

  13. 13

    WPF DataGrid Trigger on cell content

  14. 14

    How do I stop row selection when a button in DataGridTemplateColumn is clicked in wpf DataGrid

  15. 15

    WPF datagrid, autocompletebox cell

  16. 16

    WPF : Get index of clicked / selected cell on DataGrid

  17. 17

    WPF Datagrid Get Selected Cell Value

  18. 18

    Get Text of TextBox that resides inside DataGridTemplateColumn in DataGrid

  19. 19

    How to modify cell after WPF DataGrid is bound

  20. 20

    Wpf Centering Cell content in DataGridTemplateColumn

  21. 21

    wpf Datagrid Cell Formatting

  22. 22

    WPF - get row index for Combobox control in DataGridTemplateColumn

  23. 23

    How to skip WPF DataGrid cell focus and go to control inside it?

  24. 24

    WPF, datagrid cell prefixing

  25. 25

    How to edit the cell type in a DataGrid Control wpf?

  26. 26

    How to set background for single cell of datagrid wpf not single row?

  27. 27

    How to get all the rows from DataGrid in WPF

  28. 28

    Datagrid Cell content is not visible in WPF

  29. 29

    How to unselect selected cell in WPF DataGrid with SelectionUnit = Cell

HotTag

Archive