Wpf组合框文本已更改不会更改selectedvalue [英] Wpf combobox text changed does not change selectedvalue

查看:133
本文介绍了Wpf组合框文本已更改不会更改selectedvalue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在WPF UserControl中使用ComboBox控件。我把它绑定到了一个实体。问题是如果我手动输入文本(IsEditable =True)文本有时与SelectedValue的DisplayMemberPath值不匹配。



这是我的Xaml代码:

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"



这个C#公司(ItemSource):


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



(其中GetValue和SetValue再现OnNotifiPropertyChanged。)



所以,如果我有这家公司:

CompanyId:1名称:Microsoft

CompnayId:2名称:Apple



如果我手动输入Mi,ComboBox会自动选择Microsoft(SelectedValue = 1,Text = Microsoft)。但如果我删除了最后一个单词,则SelectedValue保持为1,Text变为Mi。所以我选择了一个与Id = 1的公司不对应的值。



¿任何人都试过这个问题并且知道如何解决它?



我尝试了什么:



我试过以这种方式手动更改SelectedValue:



ComboBox Xaml代码:


(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#查看代码背后:



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


           }
       }





但在这种情况下,当我更新SelectedValue时,它会自动删除文本。



我尝试的其他选项是在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.

推荐答案

这样做。更改了组合框中的值后,检查viewmodel中Companies.CurrentItem的值。如果您有任何疑问,请告诉我。

XAML代码 -

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-
<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-





ViewModel Code-

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


这篇关于Wpf组合框文本已更改不会更改selectedvalue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆