RadComboBox doesn't updates Text when renaming item

2 posts, 1 answers
  1. Michael
    Michael avatar
    1 posts
    Member since:
    Sep 2008

    Posted 23 Sep 2013 Link to this post

    I use the RadComboBox from Telerik in a WPF project.

    My Problem is that the selected text is not updated when the selected item changes its value. So when the ViewModel changes the `Name` property of the `SelectedItem`, I can not see this in the RadComboBox.

    Note: The `SelectedItem` implements `INotifyPropertyChanged` and is calling the event.


    Full source code:


    public class MainViewModel : INotifyPropertyChanged
    {
        private readonly List<Item> _items = new List<Item>();
        private Item _selectedItem;
     
        public MainViewModel()
        {
            _items.Add(new Item { Name = "Eg Zomh JywS" });
            _items.Add(new Item { Name = "Ua Qvp Lavwz" });
            _items.Add(new Item { Name = "Nee Lzx Rdaq" });
            _items.Add(new Item { Name = "Um Ztgi Yvsg" });
            _items.Add(new Item { Name = "tma Oppt fzd" });
            _items.Add(new Item { Name = "Du Zofcy Fbs" });
            _items.Add(new Item { Name = "bc Ey Ppvvcp" });
            _items.Add(new Item { Name = "Mv RSIZtf WE" });
            _items.Add(new Item { Name = "BFb YYZ PHwC" });
            _items.Add(new Item { Name = "YSW LQ DXxHu" });
        }
     
        public event PropertyChangedEventHandler PropertyChanged;
     
        public IEnumerable<Item> Items { get { return _items; } }
     
        public Item SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                OnPropertyChanged("SelectedItem");
                OnPropertyChanged("SelectedItemName");
            }
        }
     
        public string SelectedItemName
        {
            get { return _selectedItem != null ? _selectedItem.Name : null; }
            set
            {
                if (_selectedItem != null)
                {
                    _selectedItem.Name = value;
                    OnPropertyChanged("SelectedItemName");
                }
            }
        }
     
        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }


     

    public class Item : INotifyPropertyChanged
    {
        private string _name;
     
        public event PropertyChangedEventHandler PropertyChanged;
     
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }
     
        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }


     

    <Window x:Class="TelerikRadComboBoxProblem.MainWindow"
            Title="MainWindow" Height="150" Width="300">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
             
            <TextBlock Text="Search: " VerticalAlignment="Center" />
            <telerik:RadComboBox Grid.Row="0" Grid.Column="1"
                ItemsSource="{Binding Items, Mode=OneWay}"
                SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                StaysOpenOnEdit="True"
                DisplayMemberPath="Name"
                CanAutocompleteSelectItems="False"
                IsEditable="True"
                IsReadOnly="False"
                OpenDropDownOnFocus="True"
                IsFilteringEnabled="True"
                TextSearchMode="Contains">
            </telerik:RadComboBox>
             
            <TextBlock Grid.Row="1" Grid.Column="0" Text="Rename: " VerticalAlignment="Center" />
            <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding SelectedItemName, Mode=TwoWay}" />
     
        </Grid>
    </Window>


     

    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainViewModel();
        }
    }

  2. Answer
    Rosen Vladimirov
    Admin
    Rosen Vladimirov avatar
    640 posts

    Posted 26 Sep 2013 Link to this post

    Hi Michael,

    As you are using editable RadComboBox you have to bind its Text property in order to achieve the desired functionality:
    <telerik:RadComboBox Grid.Row="0" Grid.Column="1" Text="{Binding  SelectedItemName, Mode=TwoWay}"
                ItemsSource="{Binding Items, Mode=OneWay}"
                SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                .../>

    I've prepared a sample project based on your code. You can find it attached and use it for your reference.

    Hopefully this helps.

    Regards,
    Rosen Vladimirov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top