Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8270801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:41:28+00:00 2026-06-08T06:41:28+00:00

The DataGridCell does not appear to be in the VisualTree of controls. I have

  • 0

The DataGridCell does not appear to be in the VisualTree of controls.

I have a DataGridTemplateColumn that contains a Rectangle and Label in a Stack Panel inside a Grid.

<t:DataGridTemplateColumn>
    <t:DataGridTemplateColumn.CellTemplate>                                
        <DataTemplate>
            <Grid FocusManager.FocusedElement="{Binding ElementName=swatch}">
                <StackPanel Orientation="Horizontal">
                    <Rectangle Name="swatch" PreviewMouseLeftButtonDown="swatch_PreviewMouseLeftButtonDown" />
                    <Label/>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </t:DataGridTemplateColumn.CellTemplate>
</t:DataGridTemplateColumn>

I wanted the PreviewMouseLeftButtonDown event to iterate upwards through the visual tree until it finds the DataGridCell but after the Grid the parent element is null.

private void swatch_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {          
            DataGridCell cell = null;

            while (cell == null)
            {
                cell = sender as DataGridCell;
                sender = ((FrameworkElement)sender).Parent;
            }

            MethodForCell(sender);
        }

Reading the link below it seems that in the DataGrid’s Visual tree UIControls are set as the content property of the DataGridCell. So how can I get the DataGridCell from the Rectangle?

http://blogs.msdn.com/b/vinsibal/archive/2008/08/14/wpf-datagrid-dissecting-the-visual-layout.aspx

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:41:29+00:00Added an answer on June 8, 2026 at 6:41 am

    Change your eventhandler on this one:

    private void swatch_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {         
                DataGridCell cell = null;
    
                while (cell == null)
                {
                    cell = sender as DataGridCell;
                    if (((FrameworkElement)sender).Parent != null)
                        sender = ((FrameworkElement)sender).Parent;
                    else 
                        sender = ((FrameworkElement)sender).TemplatedParent;
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataGridCell that contains a ComboBox. I want, that when I fire
Have datgrid with dynamic columns number, like that: <DataGrid DataGridCell.Selected=DataGrid_GotFocus EnableColumnVirtualization=true EnableRowVirtualization=true Name=dataGrid1 DockPanel.Dock=Top
I want to know how to get the DataGridCell from DataGridCellInfo. Actually i have
I have requirement to keep datagridcell always in edit mode. I dont find any
I could not understand BorderThickness={TemplateBinding BorderThickness} . Here the code: <ControlTemplate TargetType={x:Type wpftoolkit:DataGridCell}> <Border
I have DataGrid and I set: CellStyle={StaticResource Style} My style: <Style x:Key=Style TargetType={x:Type tool:DataGridCell}
I have a wpf datagrid that has it's columns generated dynamically in code and
I am trying to change the Foreground colour of a DataGridCell, NOT the entire
I have a DataGridCheckBoxColumn that I want to set the background color of the
I have a DataGrid with template column, containing a checkbox: <DataGridTemplateColumn Header=Foreign key> <DataGridTemplateColumn.CellTemplate>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.