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

RadComboBox doesn't updates Text when renaming item

1 Answer 480 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 23 Sep 2013, 02:49 PM

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();
    }
}

1 Answer, 1 is accepted

Sort by
0
Accepted
Rosen Vladimirov
Telerik team
answered on 26 Sep 2013, 06:33 AM
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 >>
Tags
ComboBox
Asked by
Michael
Top achievements
Rank 1
Answers by
Rosen Vladimirov
Telerik team
Share this question
or