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

GridViewComboBoxColumn - ItemsSourceBinding viewmodel sample code?

3 Answers 163 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Hazzard
Top achievements
Rank 1
Hazzard asked on 04 Nov 2011, 12:51 AM
hello team,
 Is it possible with GridViewComboBoxColumn to bind to GridViewComboBoxColumn with 2 different collections via ItemSource binding?  Here is the question I posted in the Silverlight forum. http://forums.silverlight.net/p/241794/603526.aspx/1?p=True&t=634561231912631344
Visual below and current code sample of that at bottom.
Thank you very much.
Greg
Class values flow
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using Telerik.Windows.Controls;
  
namespace GridCombo
{
  
    public partial class MainPage : UserControl, INotifyPropertyChanged
    {
  
        LookupCode[] codes;
        List<SampleData> items;
  
        public MainPage()
        {
            InitializeComponent();
  
            DataContext = this;
  
            items = (from c in Enumerable.Range(0, 10)
                     select new SampleData
                     {
                         Code = "code1",
                         Name = "Some name" + c
                     }).ToList();
  
            codes = new LookupCode[4];
            codes[0] = new LookupCode
            {
                Code = "code1",
                Name = "A"
            };
            codes[1] = new LookupCode
            {
                Code = "code2",
                Name = "B"
            };
            codes[2] = new LookupCode
            {
                Code = "code3",
                Name = "C"
            };
            codes[3] = new LookupCode
            {
                Code = "code4",
                Name = "D"
            };
  
  
            InvokePropertyChanged(new PropertyChangedEventArgs("LookupCodes"));
            InvokePropertyChanged(new PropertyChangedEventArgs("Items"));
            ((GridViewComboBoxColumn)this.SampleGrid.Columns[0]).ItemsSource = this.codes;
        }
  
  
        public List<SampleData> Items
        {
            get { return items; }
        }
  
        public IEnumerable<LookupCode> LookupCodes
        {
            get { return codes; }
        }
  
        public event PropertyChangedEventHandler PropertyChanged;
  
        public void InvokePropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, e);
        }
    }
  
    public class LookupCode
    {
        public string Code { get; set; }
        public string Name { get; set; }
    }
  
    public class SampleData : INotifyPropertyChanged
    {
        private string code;
        private string name;
  
        public string Code
        {
            get
            {
                return code;
            }
            set
            {
                code = value;
                this.OnPropertyChanged("Code");
            }
        }
  
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
                this.OnPropertyChanged("Name");
            }
        }
  
        public event PropertyChangedEventHandler PropertyChanged;
  
        protected virtual void OnPropertyChanged(string name)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}
<UserControl
    xmlns:local="clr-namespace:GridCombo" 
    xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView" 
    x:Class="GridCombo.MainPage"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
  
    <Grid x:Name="LayoutRoot" Background="White" >
        <Grid.RowDefinitions>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <telerik:RadGridView x:Name="SampleGrid" Grid.Row="1" AutoGenerateColumns="False" ItemsSource="{Binding Items}">
            <telerik:RadGridView.Columns>
                <telerik:GridViewComboBoxColumn DataMemberBinding="{Binding Code, Mode=TwoWay}" Header="Class"
                                                SelectedValueMemberPath="Code" DisplayMemberPath="Name"
                                                ItemsSource="{Binding Path=LookupCodes}"/>
                <telerik:GridViewDataColumn  DataMemberBinding="{Binding Name}"/>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</UserControl>

 

3 Answers, 1 is accepted

Sort by
0
Hazzard
Top achievements
Rank 1
answered on 07 Nov 2011, 07:46 PM
I think i've discovered it's not possible bind to the combobox at runtime with different possible values for the combobox. Especially given the workarounds required for the Empty Cells in Combobox issue,
0
Accepted
Pavel Pavlov
Telerik team
answered on 08 Nov 2011, 02:15 PM
Hi Hazzard,

I am a bit confused about what you are trying to achieve after reading this thread and the other one on the SL forums.

If I understand it right , you need combo boxes on different row to be populated with different items.

There is such scenario illustrated in this help article. Please have a look at the section dedicated on the ItemsSourceBinding property. The example with the F1 teams shows a different list of  F1 pilots in each row .

Is that what you were looking for ?

Regards,
Pavel Pavlov
the Telerik team

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

0
Hazzard
Top achievements
Rank 1
answered on 08 Nov 2011, 07:44 PM
two custom controls solved the problem and solved this bug in GridViewComboBoxColumn which is documented for that control when the comboboxes are all empty, (the datacontext tug of war problem between the row and the column)

 

Tags
GridView
Asked by
Hazzard
Top achievements
Rank 1
Answers by
Hazzard
Top achievements
Rank 1
Pavel Pavlov
Telerik team
Share this question
or