Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top