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

  • Home
  • SEARCH
  • 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 4124254
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:43:01+00:00 2026-05-20T23:43:01+00:00

I’d like to access a Object from my UserControl from within the Datgrids ColumnTemplate.

  • 0

I’d like to access a Object from my UserControl from within the Datgrids ColumnTemplate.

This doesn’t work. Now I’ve read it’s because of the Datacontext.

I found this Example, which should fix this: http://blog.errorok.com/2010/09/09/212/

But the Event: ColumnDataContextChanged is never called in my Project!

here’s a part of my XAML:

<DataGridTemplateColumn Header="Database-Fieldtype" Width="Auto" IsReadOnly="False" SortMemberPath="DatabaseFieldType">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding DatabaseFieldType}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ConfigurationTool:EditProtocolDatasets}}, Path=grdDatasets.SelectedItem.Storage.DatabaseFieldTypes}" 
                      SelectedItem="{Binding DatabaseFieldType}"
                      VerticalAlignment="Top" Width="179" Padding="0" Margin="0">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}" />
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

ConfigurationTool:EditProtocolDatasets is my UserControl, grdDatasets is another Datagrid, to which SelectedItem I’d like to bind!

  • 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-05-20T23:43:02+00:00Added an answer on May 20, 2026 at 11:43 pm

    Okay, I’m going to suggest a completely different direction than my first one. My guess is that you have the ItemsSource for grdDatasets bound to something.

    For the item that’s going to act as your datacontext for the control, make sure it has the following characteristics, or at least a comparable structure:

    public class ListOfDataSets : DependencyObject
    {
      public IEnumerable<DataSetOption> Items
      {
        get
        {
          ...Whatever you normally use to get your DataSetOptions...
        }
      }
    
      public DataSetOption SelectedItem
      {
        get { return (DataSetOption)GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
      }
    
      public static readonly DependencyProperty SelectedItemProperty =
        DependencyProperty.Register("SelectedItem", typeof(DataSetOption), typeof(ListOfDataSets), new PropertyMetadata(null));
    
    }
    

    The key here is that you have one property that is a list of your choices, and another property that represents one of those items.

    Then, in your XAML, your control can have the following structure:

    <UserControl>
      <UserControl.Resources>
        <ConfigurationTool:ListOfDatasets x:Key=DataSetOptions />
      </UserControl.Resources>
      <StackPanel Name="LayoutRoot">
        <DataGrid Name="grdDatasets" 
            ItemsSource="{Binding Source={StaticResource DataSetOptions}, Path=Items}"
            SelectedItem="{Binding Source={StaticResource DataSetOptions}, Path=SelectedItem}"
            ...
        </DataGrid>
    ...
        <DataGrid Name="OtherDataGrid" ItemsSource="{Binding OtherSource}">
          <DataGrid.Columns>
        ...
        <DataGridTemplateColumn Header="Database-Fieldtype" Width="Auto" IsReadOnly="False" SortMemberPath="DatabaseFieldType">
                         <DataGridTemplateColumn.CellTemplate>
                             <DataTemplate>
                                 <TextBlock Text="{Binding DatabaseFieldType}" />
                             </DataTemplate>
                         </DataGridTemplateColumn.CellTemplate>
                         <DataGridTemplateColumn.CellEditingTemplate> 
                            <DataTemplate>
                                 <ComboBox ItemsSource="{StaticResource DataSetOptions}, Path=SelectedItem.Storage.DatabaseFieldTypes}" SelectedItem="{Binding DatabaseFieldType, Mode=TwoWay}"
                                           VerticalAlignment="Top" Width="179" Padding="0" Margin="0">
                                     <ComboBox.ItemTemplate>
                                         <DataTemplate>
                                             <TextBlock Text="{Binding Name}" />
                                         </DataTemplate>
                                     </ComboBox.ItemTemplate>
                                 </ComboBox>
                                  </DataTemplate>
                         </DataGridTemplateColumn.CellEditingTemplate>
                     </DataGridTemplateColumn>
          </Datagrid.Columns>
        </DataGrid>
      </StackPanel>
    </UserControl>
    

    I actually tried this structure out, and the databinding works fine. If the DataSetOptions change a lot, though, this solution may not work, unless you’re using MVVM, and the ViewModel is good at tracking what options are available, and presents them properly to the View.

    Hopefully this makes sense. I actually tried this one before answering.

    I was not correct with my original answer, and overestimated the capabilities of RelativeSource before I experimented with it.

    —Original text below—

    I think I can help, but I need a few more details. For now I’m just going to work off assumptions.

    Assumption 1: You’re in a WPF UserControl with a DataGrid that has a defined ItemsSource.
    Assumption 2: The UserControl has another element that you want a column within your DataGrid to have access to.

    If these two assumptions are correct, it is a much better problem to have in WPF than in Silverlight.

    Each row in your DataGrid is going to be working from within a DataContext that consists of the Item for that row. But, you can reach outside of the cell’s (or any) DataContext with a RelativeSource.

    So, if you wanted to go up the Visual Tree to get to your control’s Width, you would use:

    {Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MyUserControl}}, Path=Width}
    

    This will trace upward in the Visual Tree until an object of type “MyUserControl” is found, at which point it will grab the “Width” property and bind to it.

    The Path doesn’t have to be only one item deep, either. You can run up an down your visual tree as required. As this gets more complex, though, your code is going to be more fragile.

    If this isn’t correct, please post your XAML (or something similar) and say so, and I’ll spin up a test environment and edit my post.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
Does anyone know how can I replace this 2 symbol below from the string
I've got a string that has curly quotes in it. I'd like to replace
I have text I am displaying in SIlverlight that is coming from a CMS
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.