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 921633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:53:58+00:00 2026-05-15T18:53:58+00:00

I have a DataGridCell that contains a ComboBox. I want, that when I fire

  • 0

I have a DataGridCell that contains a ComboBox.

I want, that when I fire ‘SelectionChanged’ event of it, a CollectionViewSource of a different column (eventually – at runtime, cell) CellEditingTemplate’s Resources should be populated with data according to the selected value for this row.

Maybe DataTrigger, ActionTrigger, EventTrigger, maybe by code, XAML I don’t care, I just need a solution.

Thanks a lot!

Related: Accessing control between
DataGridCells, dynamic cascading
ComboBoxes

  • 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-15T18:53:58+00:00Added an answer on May 15, 2026 at 6:53 pm

    If I understand your question right, you will fill the contents of a combobox in a cell based on the selection of a combobox in another cell that is in the same row of the DataGrid.
    If yes:

    First Solution (IMO the preferable)

    Make a ViewModel that represents the rows data (a simple wrapper around your data object). Bind the ItemsSource-property of the destination ComboBox to a IEnumerable-property that you provide from your viewmodel.
    Bind the SelectedItem from the source-ComboBox to another property of your ViewModel. Every time this source-property changes in your ViewModel, you change the contents of the list that is provided by the ViewModel.

    Use for the desintation (list) property a ObservableCollection<T>. The source property is up to you.

    Here is an approximately example. I call the class VM (for ViewModel) but this changes nothing on your current solution. MVVM can also be used partial.

    public class DataObjectVM : DependencyObject {
    
        public static readonly DependencyProperty SelectedCategoryProperty =
            DependencyProperty.Register("SelectedCategory", typeof(CategoryClass), typeof(DataObjectVM), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,delegate (DependencyObject d,DependencyPropertyChangedEventArgs e){
                ((DataObjectVM)d).SelectedCategoryChanged(e);
            }));
    
        ObservableCollection<ItemClass> _items=new ObservableCollection<ItemClass>();
    
    
        void SelectedCategoryChanged(DependencyPropertyChangedEventArgs e) {
    
            // Change here the contents of the _items collection. 
            // The destination ComboBox will update as you desire
            // Do not change the _items reference. Only clear, add, remove or
            //  rearange the collection-items
    
        }
    
        // Bind the destination ComboxBox.ItemsSource to this property
        public IEnumerable<ItemClass> DestinationItems {
            get {
                return _items;
            }
        }
        // Bind to this property with the source ComboBox.SelectedItem
        public CategoryClass SelectedCategory {
            get { return (CategoryClass)GetValue(SelectedCategoryProperty); }
            set { SetValue(SelectedCategoryProperty, value); }
        }
    
    }
    

    Add a constructor to this class that takes your data object and make some wrapper properties to the rest the properties you need to provide in the DataGrid. If they are alot, you can also make one property that provides your data object and the bind directly to it. Not nice, but it will do the job.
    You also can (must) pre-initialize the SelectedCategory with data from your business object. Do this also in the constructor.
    As a ItemsSource for the DataGrid you give an IEnumerable of the DataObjectVM-class that wrapps all items you want to show.


    Alternative way with VisualTreeHelper

    If you want to do it manual, register in the code behind a handler for the ComboBox.SelectionChangedEvent and change then the ItemsSource of the destination ComboBox manual. The business-object you will get with the EventArgs. The destination ComboBox you must search in the visual tree (Use the VisualTreeHelper). The events can be wired also if you use the DataGridTemplateColumn class and add a DataTemplate with the corresponding ComboBoxes.

    But I think this is realy not very simple to do and can be error prone. The above solution is much easier.

    Here is the code you propably are looking for:

    private void CboSource_SelectionChanged(object sender, SelectionChangedEventArgs e) {
        ComboBox cbo = (ComboBox)sender;
        FrameworkElement currentFe = VisualTreeHelper.GetParent(cbo) as FrameworkElement;
        while (null != currentFe && !(currentFe is DataGridRow)) {
            currentFe = VisualTreeHelper.GetParent(currentFe) as FrameworkElement;
        }
        if (null != currentFe) {
            List<ComboBox> list = new List<ComboBox>();
            FindChildFrameworkElementsOfType<ComboBox>(currentFe,list);
            // Requirement 1: Find ComboBox
            foreach (ComboBox cboFound in list) {
                if (cboFound.Name == "PART_CboDestination") {
                    // This is the desired ComboBox
                    // Your BO is available through cbo.Found.DataContext property
                    // If don't like to check the name, you can also depend on the
                    // sequence of the cbo's because I search them in a deep search
                    // operation. The sequence will be fix.
                }
            }
    
            List<DataGridCell> cells = new List<DataGridCell>();
            FindChildFrameworkElementsOfType<DataGridCell>(currentFe,cells);
            // Requirement 2: Find Sibling Cell
            foreach (DataGridCell cell in cells) {
                // Here you have the desired cell of the other post
                // Take the sibling you are interested in
                // The sequence is as you expect it
    
                DataGridTemplateColumn col=cell.Column as DataGridTemplateColumn;
                DataTemplate template = col.CellTemplate;
    
                // Through template.Resources you can access the CollectionViewSources
                // if they are placed in the CellTemplate.
                // Change this code if you will have an edit cell template or another
                // another construction
    
            }
        }            
    }
    
    void FindChildFrameworkElementsOfType<T>(DependencyObject parent,IList<T> list) where T: FrameworkElement{            
        DependencyObject child;
        for(int i=0;i< VisualTreeHelper.GetChildrenCount(parent);i++){            
            child = VisualTreeHelper.GetChild(parent, i);
            if (child is T) {
                list.Add((T)child);
            }
            FindChildFrameworkElementsOfType<T>(child,list);
        }
    }
    

    And this is the markup I used:

    <DataGrid.Columns>                        
        <DataGridTemplateColumn Header="Source" >
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox Name="PART_CboSource" SelectionChanged="CboSource_SelectionChanged" ItemsSource="!!YOUR ITEMS SOURCE!!" SelectedItem="{Binding Category}">                            
                    </ComboBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Destination">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox Name="PART_CboDestination"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
    

    Accessing the CollectionViewSource

    To access the CollectionViewSource, put it into the resources section of the corresponding DataTemplate, not of the panel, then you will have direct access to them. IMO is this location anyway more appropriate than the resources-container of the grid.

    If you dont’t want to do this, check the state of the following post:

    How to get logical tree of a DataTemplate

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

Sidebar

Related Questions

I want to know how to get the DataGridCell from DataGridCellInfo. Actually i have
I have a DataGridCheckBoxColumn that I want to set the background color of the
I have a datagrid in wpf, my cell is <DataGridTextColumn.CellStyle> <Style TargetType=DataGridCell> <Setter Property=Background
Have datgrid with dynamic columns number, like that: <DataGrid DataGridCell.Selected=DataGrid_GotFocus EnableColumnVirtualization=true EnableRowVirtualization=true Name=dataGrid1 DockPanel.Dock=Top
I have a DataGrid that two of its columns are ComboBoxes (one contains few
Currently I have the following code to flash a DataGrid cell when it's value
Have following listener for keyboard ArrowDown event(it's key code is 40 ): window.onload =
I am using a WPF c# application. I have a user control which contains
Have finally got a responsive site working (of a fashion). What I want to
I have a wpf datagrid that has it's columns generated dynamically in code and

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.