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

ComboBox Column and ItemsSourceBinding, changes not commited

7 Answers 126 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 20 Sep 2012, 08:11 AM
I'm trying to use ItemsSourceBinding. The list is displayed but the selected item is not commited to binded property (see attached picture).
XAML:
<telerik:RadGridView Name="playersGrid"
                             ItemsSource="{Binding Players}"
                             AutoGenerateColumns="False">
 
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Number}"/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Position}"/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Country}" IsReadOnlyBinding="{Binding IsReadOnlyProperty}"/>
 
                <telerik:GridViewComboBoxColumn DataMemberBinding="{Binding ClubID}"                                                                               
                                                SelectedValueMemberPath="ID"
                                                ItemsSourceBinding="{Binding AvailableClubs}"
                                                DisplayMemberPath="Name" />
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>

ViewModel:
/// <summary>
/// A football player.
/// </summary>
public class Player : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
 
    private string name;
    private int number;
    private Position position;
    private string country;
 
 
    private bool isReadOnlyProperty;
    public bool IsReadOnlyProperty
    {
        get { return this.isReadOnlyProperty; }
        set
        {
            if (value != this.isReadOnlyProperty)
            {
                this.isReadOnlyProperty = value;
                this.OnPropertyChanged("IsReadOnlyProperty");
            }
        }
    }
    private int clubID;
    public int ClubID
    {
        get { return this.clubID; }
        set
        {
            if (value != this.clubID)
            {
                this.clubID = value;
                this.UpdateIsReadOnlyProperty(this.clubID);
                this.OnPropertyChanged("ClubID");
 
            }
        }
    }
 
 
    public string Name
    {
        get { return this.name; }
        set
        {
            if (value != this.name)
            {
                this.name = value;
                this.OnPropertyChanged("Name");
            }
        }
    }
 
    public int Number
    {
        get { return this.number; }
        set
        {
            if (value != this.number)
            {
                this.number = value;
                this.OnPropertyChanged("Number");
            }
        }
    }
 
    public Position Position
    {
        get { return this.position; }
        set
        {
            if (value != this.position)
            {
                this.position = value;
                this.OnPropertyChanged("Position");
            }
        }
    }
 
    public string Country
    {
        get { return this.country; }
        set
        {
            if (value != this.country)
            {
                this.country = value;
                this.OnPropertyChanged("Country");
            }
        }
    }
 
    public IEnumerable<Club> AvailableClubs
    {
        get
        {
            return from c in Club.GetClubs()
                   where c.ID == this.ClubID
                   select c;
        }
    }
 
    public Player()
    {
 
    }
 
    public Player(string name, int number, Position position, string country, int clubID, bool isReadOnlyProperty)
    {
        this.name = name;
        this.number = number;
        this.position = position;
        this.country = country;
        this.clubID = clubID;
        this.isReadOnlyProperty = isReadOnlyProperty;
    }
 
    protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, args);
        }
    }
 
    private void OnPropertyChanged(string propertyName)
    {
        this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }
 
    public override string ToString()
    {
        return this.Name;
    }
 
    public static ObservableCollection<Player> GetPlayers()
    {
        return new ObservableCollection<Player>(Club.GetClubs().SelectMany(c => c.Players));
    }
 
    private void UpdateIsReadOnlyProperty(int clubID)
    {
        if (clubID == 2 || clubID == 3)
        {
            this.IsReadOnlyProperty = true;
        }
        else
        {
            this.IsReadOnlyProperty = false;
        }
    }
}

This sample is based on SL sample attached by Maya here: http://www.telerik.com/community/forums/silverlight/gridview/binding-from-radgridview-to-radcombobox.aspx 

7 Answers, 1 is accepted

Sort by
0
Nick
Telerik team
answered on 21 Sep 2012, 12:22 PM
Hello Alex,

Have you tried setting the SelectedValueMemberPath to "ClubID"?

Greetings,
Nik
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

0
Alex
Top achievements
Rank 1
answered on 21 Sep 2012, 12:37 PM
I can not understand how this will help. Look, AvailableClubs binded to combobox column ItemsSourceBinding is list of Club instances. In class club there is no "ClubID" property. Its identifier is ID property. So SelectedValueMemberPath set to "ID". ClubID is property of Player class, which is used as grid row context. So we bind column DataMemberBinding to this property.
But nevertheless I checked your advice, he predictably does not help.
I can not attach here but I can send you ready project so you can easily reproduce this problem.
0
Nick
Telerik team
answered on 21 Sep 2012, 01:08 PM
Hello Alex,

I am sorry for misdirecting you. It would be great if you could provide the sample project you are using via support ticket. 

Looking forward to hearing from you! 

Regards,
Nik
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

0
Alex
Top achievements
Rank 1
answered on 21 Sep 2012, 01:20 PM
0
Nick
Telerik team
answered on 24 Sep 2012, 11:29 AM
Hi Alex,

I have looked into your project and I believe I have found the problem. The itemssourse you give to the combo box column consists only of clubs that have the same id as the one of the player in the current row. Therefore, the lookup value in the combo box and the bound one are the same - therefore there isn't a change fired when you change the club. 
Now, you can see that if you modify the Clubs collection to contain other clubs as well, everything works as expected. 

return  (from c in Club.GetClubs()
        //where c.ID == this.ClubID
        select c).ToList();
Copy Code

Hope this makes sense! 


Regards,
Nik
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

0
Ravindra
Top achievements
Rank 1
answered on 11 Jun 2018, 10:21 AM

Hi Alex,

In this example, for the combo box item selection , property changed event is not triggered until we press enter or loose the focus.
How the property value will be changed for the selected item directly after selecting it from the combobox without loosing the focus or pressing enter key.

0
Dinko
Telerik team
answered on 13 Jun 2018, 08:53 AM
Hello Ravindra,

What I can suggest you is to add a handler for the SelectionChanged event of RadComboBox (which is the editing element of GridViewComboBoxColumn). In the event handler, you can commit the selected object using the CommitEdit() method of the RadGridView.
public MainWindow()
{
    InitializeComponent();
    this.AddHandler(RadComboBox.SelectionChangedEvent, new SelectionChangedEventHandler(OnSelectionChanged));
}
 
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (e.RemovedItems.Count > 0)
    {
        this.myGrid.CommitEdit();
    }
}

Regards,
Dinko
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
Alex
Top achievements
Rank 1
Answers by
Nick
Telerik team
Alex
Top achievements
Rank 1
Ravindra
Top achievements
Rank 1
Dinko
Telerik team
Share this question
or