Click here to Skip to main content
15,566,394 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am using a ComboBox control in a WPF UserControl. I have binded it to a Entity. The problem is if I type text manually (IsEditable = "True") the Text sometimes does not match with the DisplayMemberPath Value of the SelectedValue.

This is my Xaml Code:
ItemsSource="{Binding Companies, Mode=OneWay}"
                                      DisplayMemberPath="Name"
                                      SelectedValue = "{Binding SelectedItem.CompanyId, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
                                      SelectedValuePath="CompanyId"
                                      IsSynchronizedWithCurrentItem="True"
                                      IsEditable="True"

And this C# Companies (ItemSource):
public class CompanyEntity : BaseEntity
    {
                
        public int? CompanyId { get; set; }
        public string Name
        {
            get
            {
                return GetValue(() => Name);
            }
            set
            {
                SetValue(() => Name,value);
            }
        }
}

(where GetValue and SetValue reproduces OnNotifiPropertyChanged.)

So, if I have this companies:
CompanyId:1 Name: Microsoft
CompnayId:2 Name: Apple

If I type manually "Mi", the ComboBox automatically select "Microsoft (SelectedValue = 1, Text = Microsoft). But If I delete the last word, SelectedValue remains 1 and Text changes to "Mi". So I have selected a value that does not correspond to Company with Id=1.

¿Anyone has experimented this issue and knows hoy to resolve it?

What I have tried:

I tried to change SelectedValue manually on this way:

ComboBox Xaml Code:
TextBoxBase.TextChanged="ComboBox_TextChanged"


C# view code behind:
private void ComboBox_TextChanged(object sender, TextChangedEventArgs e)
       {
           if (sender is ComboBox)
           {
               ComboBox combo = sender as ComboBox;

               if (!String.IsNullOrWhiteSpace(combo.Text))
               {
                   if (combo.SelectedValue == null || (int)combo.SelectedValue == 0)
                       return;
                   else
                   {
                       var propertyInfo =                    combo.SelectedItem.GetType().GetProperty(combo.DisplayMemberPath);
                       string value = propertyInfo.GetValue(combo.SelectedItem, null).ToString();
                       if (!value.Equals(combo.Text, StringComparison.OrdinalIgnoreCase))
                       {
                           combo.SelectedValue = 0;
                       }
                   }
               }


           }
       }


But in this case, when I update SelectedValue it automatically deletes Text.

Other option I have tried is updating Text after SelectedValue = 0, but then the cursor moves to the begining, whit a poor user experience.
Posted
Updated 29-Sep-16 6:42am
v3

1 solution

Do Like this. After changes the value in combobox then check the value for "Companies.CurrentItem" in viewmodel. If you have any doubt please let me know.
XAML Code-
HTML
<ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="56,79,0,0"
         ItemsSource="{Binding Companies, Mode=OneWay}"
                                      DisplayMemberPath="Name"
                                      SelectedValue = "{Binding SelectedItem.CompanyId, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
                                      SelectedValuePath="CompanyId"
                                      IsSynchronizedWithCurrentItem="True"
                                      IsEditable="True" VerticalAlignment="Top" Width="222"/> 


ViewModel Code-

C#
public class CompanyEntity
    {

        public int? CompanyId { get; set; }
        public string Name { get; set; }
    }
   
    class MainWindowVM
    {
        ObservableCollection<CompanyEntity> companies = new ObservableCollection<CompanyEntity>();
        ICommand saveCommand = null;
        public MainWindowVM()
        {
            companies.Add(new CompanyEntity { CompanyId = 1,Name= "Microsoft" });
            companies.Add(new CompanyEntity { CompanyId = 2,Name= "Apple" });
        }
        public ICommand SaveCommand
        {
            get
            {
                return saveCommand = new RelayCommand(Save);
            }
        }

        private void Save()
        {
            
        }

        public CollectionView Companies
        {
            get
            { 
                return CollectionViewSource.GetDefaultView(companies) as CollectionView;
            }
        }
    }
 
Share this answer
 
v8

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900