This is a migrated thread and some comments may be shown as answers.

Dynamically set RadComboBox ItemsSource

4 Answers 603 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mattias
Top achievements
Rank 1
Mattias asked on 20 Jan 2012, 10:28 AM
A combobox in my gridview needs to have different ItemsSource depending on the value of another column.
I've tried to solve this by listening on the gridview's PreparingCellForEdit event:
private void RadGridView_PreparingCellForEdit(object sender, GridViewPreparingCellForEditEventArgs e)  
{  
    switch (e.column.Name) 
    
        case "MyDynamicColumn"
            var combobox = e.EditingElement as RadComboBox;  
            combobox.ItemsSource = List<string> { "1", "2", "3" }; //TODO: Replace with some dynamic business logic here  
            combobox.SetBinding(RadComboBox.SelectedValueProperty, "MyComboValue");   
            break
        
    
}

...and this kind-of works. The combobox displays its SelectedValue once it is selected and the dropdown shows the correct list of values.

However, when the combobox is not in edit-mode, it looks empty. I'd like to bind a TextBlock with the SelectedValue for display, but don't know how to accomplish that.

Any thoughts would be appreciated.

4 Answers, 1 is accepted

Sort by
0
Accepted
Vlad
Telerik team
answered on 20 Jan 2012, 10:35 AM
Hi,

 It will be better if you create custom column, override CreateCellElement/CreateCellEditElement and create/bind the combo in the way you want. 

Regards,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Mattias
Top achievements
Rank 1
answered on 20 Jan 2012, 02:03 PM
Thanks for your input. This works fine.

For future documentation, in case others have the same problem, this is what I've done:

using System;
using System.Windows;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
  
namespace MyProject.Helpers
{
    public class CellElementCreatedEventArgs : EventArgs
    {
        public GridViewCell Cell { get; set; }
        public GridViewColumn Column { get; set; }
        public FrameworkElement Element { get; set; }
        public object DataItem { get; set; }
    }
  
    public class DynamicComboBoxColumn : GridViewComboBoxColumn
    {
        public event EventHandler<CellElementCreatedEventArgs> CellElementCreated;
        protected void OnCellElementCreated(CellElementCreatedEventArgs e)
        {
            if (CellElementCreated != null)
                CellElementCreated(this, e);
        }
  
        public override System.Windows.FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
        {
            var element = base.CreateCellElement(cell, dataItem);
            var e = new CellElementCreatedEventArgs { Cell = cell, Column = cell.Column, Element = element, DataItem = dataItem };
            OnCellElementCreated(e);
            return element;
        }
  
        public override System.Windows.FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem)
        {
            var element = base.CreateCellEditElement(cell, dataItem);
            var e = new CellElementCreatedEventArgs { Cell = cell, Column = cell.Column, Element = element, DataItem = dataItem };
            OnCellElementCreated(e);
            return element;
        }
    }
}

And then added GridView to xaml-page:

<telerik:RadGridView ItemsSource="{Binding GridViewItemsSource}" AutoGenerateColumns="False">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Header="Id" DataMemberBinding="{Binding ID}"/>
        <helpers:DynamicComboBoxColumn Header="Dynamic 1" x:Name="Dynamic1" 
                DataMemberBinding="{Binding DynValue1}" 
                CellElementCreated="RadGridView_CellElementCreated"/>
        <helpers:DynamicComboBoxColumn Header="Dynamic 2" x:Name="Dynamic2" 
                DataMemberBinding="{Binding DynValue2}" 
                CellElementCreated="RadGridView_CellElementCreated"/>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>
(Don't forget to add xmlns:helpers="clr-namespace:MyProject.Helpers" to the top of the xaml-page)

And finally in the xaml code-behind:

private void RadGridView_CellElementCreated(object sender, CellElementCreatedEventArgs e)
{
    var rowItem = e.DataItem as MyData;
    switch (e.Column.Name)
    {
        case "Dynamic1":
            SetItemsSource(e.Element, (this.DataContext as MyViewModel).GetDynData1(rowItem.ID));
            break;
        case "Dynamic2":
            SetItemsSource(e.Element, (this.DataContext as MyViewModel).GetDynData2(rowItem.ID));
            break;
    }
}
  
/// <summary>
/// Sets the ItemsSource dynamically for Netting.Helpers.DynamicComboBoxColumn's
/// </summary>
/// <param name="element"></param>
/// <param name="itemsSource"></param>
void SetItemsSource(FrameworkElement element, IEnumerable itemsSource)
{
    if (element is LookupElement)
    {
        (element as LookupElement).ItemsSource = itemsSource;
    }
    else if (element is ItemsControl)
    {
        var oldBinding = element.GetBindingExpression(Selector.SelectedValueProperty);
        (element as ItemsControl).ItemsSource = itemsSource;
        if (oldBinding != null)
            element.SetBinding(Selector.SelectedValueProperty, oldBinding.ParentBinding.Path.Path);
    }
    else
        throw new NotSupportedException();
}
0
Guillaume
Top achievements
Rank 1
answered on 28 Feb 2013, 08:19 PM
Hi,
I'm using Mattias solution to get a GridViewComboBoxColumn cell to bind to a dynamic collection. My GridView is inside a Tabcontrol. The first time the GridView is displayed, my GridViewComboBoxColumn cell displays the selected item correctly. But when I switch tab and come back on the GridView, the GridViewComboBoxColumn cell doesn't display the selected item. If I click that cell for editing, the selected item is displayed.

Any idea on how to fix this problem? Should I use another method to link GridViewComboBoxColumn to a dynamic collection?

I'm using Telerik WPF Q2 2012.

Thanks

0
Yoan
Telerik team
answered on 05 Mar 2013, 11:24 AM
Hi Guillaume,

Can you please check this troubleshooting article on this matter. I hope you find it useful. 

Regards,
Yoan
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Mattias
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Mattias
Top achievements
Rank 1
Guillaume
Top achievements
Rank 1
Yoan
Telerik team
Share this question
or