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

Changing ItemSource with SelectedValue binding

7 Answers 265 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Chad
Top achievements
Rank 1
Chad asked on 14 Jan 2011, 06:37 PM
If I have a class such as:

class Table
{
    public string Name;
    public string GroupBy;
    public ColumnList Columns;
}

I have a listbox bound to a list of Table objects.  When a table is selected, I want to show a combobox that will let the user pick from the list columns and set Table.GroupBy to the SelectedColumn.Name.

<telerik:RadComboBox DisplayMemberPath="DisplayName" SelectedValuePath="Name"
SelectedValue="{Binding SelectedTable.GroupBy, Mode=TwoWay}"
ItemsSource="{Binding SelectedTable.Columns}"></telerik:RadComboBox>

Sets the selected value before the ItemsSource is loaded, so the value is not displayed.


<telerik:RadComboBox DisplayMemberPath="DisplayName" SelectedValuePath="Name"
ItemsSource="{Binding SelectedTable.Columns}"
SelectedValue="{Binding SelectedTable.GroupBy, Mode=TwoWay}" ></telerik:RadComboBox>

When SelectedTable is changed, the ItemsSource is cleared out first and GroupBy is set to null.


What is the correct way to handle this scenario?

7 Answers, 1 is accepted

Sort by
0
George
Telerik team
answered on 20 Jan 2011, 09:30 AM
Hello Chad,

Could you please give us some details about ColumnList type? If you send us a sample project that reproduces your scenario, it would be very helpful.

I am glad to assist you further. 

Best wishes,
George
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Chad
Top achievements
Rank 1
answered on 20 Jan 2011, 03:05 PM
ColumnList is an observable collection of Columns:

class Column
{
    public string DisplayName;
    public string Name;
}

All of my properties implement INotifyPropertyChanged.  The silverlight combobox has the same bug:  https://connect.microsoft.com/VisualStudio/feedback/details/523394/silverlight-forum-combobox-selecteditem-binding

The workaround that works for me is:

public class ComboBox : System.Windows.Controls.ComboBox

    protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
    { 
        var bindingExpression = GetBindingExpression(SelectedValueProperty); 
        base.OnItemsChanged(e); 
        if (bindingExpression != null) 
        { 
            var binding = bindingExpression.ParentBinding; 
            SetBinding(SelectedValueProperty, bindingExpression.ParentBinding); 
        } 
    }
}

This workaround doesn't work if I inherit from telerik's RadComboBox.
0
George
Telerik team
answered on 26 Jan 2011, 03:09 PM
Hello Chad,

Could you please send us a sample project that reproduces this problem? This will definitely helps us in further pinpointing the problem. 

Best wishes,
George
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Chad
Top achievements
Rank 1
answered on 01 Feb 2011, 11:39 PM
MainPage.zaml:

<UserControl x:Class="ShowColumnsOutsideTheGrid.MainPage"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:my="clr-namespace:ShowColumnsOutsideTheGrid"
             mc:Ignorable="d" d:DesignHeight="700" d:DesignWidth="700">
    <UserControl.Resources>
        <my:MyViewModel x:Key="MyViewModel"/>
    </UserControl.Resources>
    <Grid DataContext="{StaticResource MyViewModel}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
          
        <ListBox ItemsSource="{Binding Tables}" DisplayMemberPath="Name"
                 Grid.Column="0" x:Name="tableList"></ListBox>
          
        <Grid DataContext="{Binding SelectedItem,ElementName=tableList}"
              Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </Grid.RowDefinitions>
              
            <StackPanel Orientation="Horizontal" Grid.Row="0">
                <TextBlock Text="Table Name:" VerticalAlignment="Center"></TextBlock>
                <TextBox Text="{Binding Name, Mode=TwoWay}"></TextBox>
            </StackPanel>
  
            <StackPanel Orientation="Horizontal" Grid.Row="1">
                <TextBlock Text="Group table by column:" VerticalAlignment="Center"></TextBlock>
                <telerik:RadComboBox ItemsSource="{Binding Columns}" DisplayMemberPath="DisplayName" SelectedValuePath="Name"
                                     SelectedValue="{Binding GroupBy, Mode=TwoWay}"></telerik:RadComboBox>
            </StackPanel>
        </Grid>
    </Grid>
</UserControl>


MyViewModel.cs:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
  
namespace ShowColumnsOutsideTheGrid
{
    public class Table: PropertyChangedBase
    {
        private string vName;
        public string Name
        {
            get { return vName; }
            set
            {
                vName = value;
                OnPropertyChanged("Name");
            }
        }
                  
  
        private string vGroupBy;
        public string GroupBy
        {
            get { return vGroupBy; }
            set
            {
                vGroupBy = value;
                OnPropertyChanged("GroupBy");
            }
        }
  
  
        private ObservableCollection<Column> vColumns;
        public ObservableCollection<Column> Columns
        {
            get { return vColumns; }
            set
            {
                vColumns = value;
                OnPropertyChanged("Columns");
            }
        }
    }
  
    public class Column: PropertyChangedBase
    {
        private string vDisplayName;
        public string DisplayName
        {
            get { return vDisplayName; }
            set
            {
                vDisplayName = value;
                OnPropertyChanged("DisplayName");
            }
        }
  
        private string vName;
        public string Name
        {
            get { return vName; }
            set
            {
                vName = value;
                OnPropertyChanged("Name");
            }
        }
    }
  
  
    public class PropertyChangedBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
  
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, args);
            }
        }
  
        protected void OnPropertyChanged(string propertyName)
        {
            this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
    }
  
    public class MyViewModel : PropertyChangedBase
    {
        private ObservableCollection<Table> vTables { get; set; }
        public ObservableCollection<Table> Tables
        {
            get { return vTables; }
            set
            {
                vTables = value;
                OnPropertyChanged("Tables");
            }
        }
  
        public MyViewModel()
        {
            LoadData();
        }
  
        private void LoadData()
        {
            Tables = new ObservableCollection<Table>();
            for (int t = 0; t < 10; t++)
            {
                Table table = new Table();
                table.Name = "Table " + t.ToString();
                table.Columns = new ObservableCollection<Column>();
  
                for (int c = 0; c < 5; c++)
                {
                    Column column = new Column();
                    column.DisplayName = "Field " + c.ToString();
                    column.Name = "table " + t.ToString() + ", Column " + c.ToString();
                    table.Columns.Add(column);
                }
  
                // pick column 3 for the group by
                table.GroupBy = table.Columns[3].Name;
  
                Tables.Add(table);
            }
        }
    }
}

It's quick and dirty code, but it shows the bug.  Pick table 1 in the listbox on the left and notice how Field 3 is selected in the combobox.  Now, select table 2 in the listbox and Field 3 will be selected in the combobox.  Select, table 1 in the listbox again and notice that nothing is selected in the combobox!
0
George
Telerik team
answered on 07 Feb 2011, 01:05 PM
Hi Chad,

 
Thank you sending us the code. We are aware of this issue with RadComboBox. Unfortunately, there is no workaround at this stage. We will fix this issue in our future releases.

Best wishes,
George
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Christophe
Top achievements
Rank 2
answered on 07 Mar 2011, 04:44 PM
Hi,

We have the same problem. When will you fix this issue ?

BR
Christophe
0
Valeri Hristov
Telerik team
answered on 08 Mar 2011, 11:33 AM
Hello Christophe,

I cannot commit to a specific date for this feature, but we will do our best to provide a solution for this problem by the Q2 2011 release, which is expected in the summer.

Regards,
Valeri Hristov
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
ComboBox
Asked by
Chad
Top achievements
Rank 1
Answers by
George
Telerik team
Chad
Top achievements
Rank 1
Christophe
Top achievements
Rank 2
Valeri Hristov
Telerik team
Share this question
or