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

ItemsSource with integers in ComboBoxColumn doesn't work

6 Answers 124 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Lukas
Top achievements
Rank 1
Lukas asked on 06 Jul 2012, 09:55 AM
Let's say we have something like this.

using System.Collections.ObjectModel;
 
namespace ComboBoxColumn
{
    public class Record
    {
        public int Weight { get; set; }
    }
 
    public partial class MainPage
    {
        public ObservableCollection<Record> Records { get; set; }
        public ObservableCollection<int> Weights { get; set; }
 
        public MainPage()
        {
            InitializeComponent();
            Weights = new ObservableCollection<int> {10, 50, 100};
            Records = new ObservableCollection<Record>
                {
                    new Record
                        {
                            Weight = Weights[0]
                        },
                    new Record
                        {
                            Weight = Weights[1]
                        },
                };
            DataContext = this;
        }
    }
}

XAML

<UserControl x:Class="ComboBoxColumn.MainPage"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
    <Grid x:Name="LayoutRoot">
        <telerik:RadGridView ItemsSource="{Binding Records}" AutoGenerateColumns="False">
            <telerik:RadGridView.Columns>
                <telerik:GridViewComboBoxColumn ItemsSource="{Binding Path=Weights}" DataMemberBinding="{Binding Weight}"
                                                Header="Enum"/>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</UserControl>


We create a couple of records and put them into the ItemsSource of a RadGridView. In there we define a ComboBoxColumn. We want an external list of integers as the ItemsSource of the combo box. We "reference" a value from the list which is bound to the ItemsSource of the ComboBox. But it doesn't work at all. I also experience this issue with Enum values (=> value types).

Can somebody tell me why? Is there a reason, that just actual objects work?

6 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 06 Jul 2012, 10:50 AM
Hello,

Please keep in mind that the DataContext of the cell is not the DataContext of the 'LayoutRool', but the business object associated to the row.  In your case the business object related to the row is Record. As the Weights is not defined under the Record object, you should set the right Source as shown in this help article

Greetings,
Didie
the Telerik team

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

0
Lukas
Top achievements
Rank 1
answered on 10 Jul 2012, 07:32 AM
I thought the ItemsSources DataContext is the one of LayoutRoot, since the last changes. I thought only the ItemsSourceBinding property has the DataContext of the record. Or am I wrong with that?
0
Dimitrina
Telerik team
answered on 10 Jul 2012, 08:18 AM
Hi,

Actually the DataContext for the GridViewComboBoxColumn is not the one of the LayoutRoot, it is still the DataContext of the underlying record. Please keep in mind that is not reliable to use it though. Problems could occur unless you have specified a valid Source for the Binding as suggested previously.

Detailed information on GridViewComboBoxColumn and all the properties related to it is available in this online article.

I hope this explains better. Please let me know if you have any further questions.

Kind regards,
Didie
the Telerik team

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

0
Lukas
Top achievements
Rank 1
answered on 10 Jul 2012, 08:47 AM
Okay, but now please tell me why this works with a list of strings, like with the code I provided here. Obviously the ItemsSource property finds properties from the outer data context.

You provided this help article. One suggestion was "1. Expose the ViewModel as a static resource on the page so that it can be easily accessible by the binding". We've been using this for quite some time - but if I remember right, you need to make these lists static, as well, which makes everything quite unhandy. I think since SL5 the approach via RelativeSource is way more elegant. Like this:

<telerik:GridViewComboBoxColumn Header="Category"
            DataMemberBinding="{Binding CategoryID}"
            ItemsSource="{Binding DataContext.Categories, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
            DisplayMemberPath="CategoryName"
            SelectedValueMemberPath="CategoryID" />

This requires that the UsersControls DataContext is properly set.
0
Accepted
Dimitrina
Telerik team
answered on 10 Jul 2012, 10:33 AM
Hello,

 It is not recommended to work with the RelativeSource as the Name scope is not reliable. Since the RadGridView supports UI Virtualization, its rows/cells are reused and that is why we cannot rely on the visual elements.
 

Kind regards,
Didie
the Telerik team

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

0
Lukas
Top achievements
Rank 1
answered on 11 Jul 2012, 12:59 PM
Thanks for your help (not just this thread :)).

I have chosen the approach via code behind. Accessing the ViewModel through a StaticResource isn't possible because the ViewModel's ctor isn't parameterless.
Tags
GridView
Asked by
Lukas
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Lukas
Top achievements
Rank 1
Share this question
or