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

RadComboBox backspace clears control but doesn't select 'null' value object

2 Answers 223 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 22 Dec 2016, 03:17 PM

I have seen multiple other posts about this (e.g. http://www.telerik.com/forums/backspace-clears-the-combobox-selecteditem) but I don't think any of them are asking the same question...

I have a RadComboBox which contains a collection of objects (i.e. it's not editable). I have set the SelectedValuePath to indicate which property contains the value that SelectedValue should represent. One of the items has a "null" as the value, so when the value that it is bound to is "null", this is the item which appears. I'm using MVVM to bind to a ViewModel, but I don't think that affects the issue.

When the backspace button is pressed, the ViewModel's property which is bound to SelectedValue is set to null (this doesn't happen if the "null" value object was the one that was selected at the time). But the issue is that the box contents are cleared. Since the SelectedValue is now null, I would expect the control to be showing the item whose SelectedValue is "null". Two good reasons why it ought to do that...

1) symmetry: that is what happens when the window first opens if the bound value is "null"

2) when the "null" value object was the one that was selected when backspace was pressed, the control didn't bother mentioning to the change to the bound ViewModel, so *it* believes that nothing has changed; but the UI now does not correspond with the SelectedValue.

Here is a really simplistic example:

Create a new WPF application.

Replace MainWindow.xaml with...

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication4"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:MainWindowViewModel/>
    </Window.DataContext>
   
    <Grid>
        <telerik:RadComboBox SelectedValuePath="Tag" SelectedValue="{Binding MySelectedValue}">
            <telerik:RadComboBox.Items>
                <telerik:RadComboBoxItem Content="Hello" Tag="Hello"/>
                <telerik:RadComboBoxItem Content="This" Tag="{x:Null}"/>
                <telerik:RadComboBoxItem Content="Is" Tag="Is"/>
                <telerik:RadComboBoxItem Content="A" Tag="A"/>
                <telerik:RadComboBoxItem Content="Test" Tag="Test"/>
            </telerik:RadComboBox.Items>
        </telerik:RadComboBox>
    </Grid>
</Window>

--Ends--

Add "MainWindowViewModel.cs", contents follow...

using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace WpfApplication4
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        private object _mySelectedValue;
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public object MySelectedValue
        {
            get
            {
                return _mySelectedValue;
            }
            set
            {
                _mySelectedValue = value;
                OnPropertyChanged();
            }
        }
    }
}

--Ends--

2 Answers, 1 is accepted

Sort by
0
Accepted
Dilyan Traykov
Telerik team
answered on 27 Dec 2016, 11:01 AM
Hello Richard,

What I can suggest in this scenario in order to achieve the desired behavior is to use an approach similar to the one my colleague offered in the forum thread you referenced and handle the SelectionChanged event like so:

private void RadComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var comboBox = sender as RadComboBox;
 
    if (comboBox != null)
    {
        if (comboBox.SelectedItem == null && e.RemovedItems.Count > 0)
        {
            foreach (var item in comboBox.Items)
            {
                if ((item as RadComboBoxItem).Tag == null)
                {
                    comboBox.SelectedItem = item;
                    break;
                }
            }
        }
    }
}

Please let me know whether this would work for you.

Regards,
Dilyan Traykov
Telerik by Progress
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 you to write beautiful native mobile apps using a single shared C# codebase.
0
Richard
Top achievements
Rank 1
answered on 28 Dec 2016, 09:16 AM
Thanks, yes, that works. I've derived a class from RadComboBox and put the code in there, so that I don't have to duplicate this all over the place.
Tags
ComboBox
Asked by
Richard
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Richard
Top achievements
Rank 1
Share this question
or