检索Combobox Selected项目到Textbox有问题 [英] Have problem in retrieving Combobox Selected item to Textbox

查看:64
本文介绍了检索Combobox Selected项目到Textbox有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我正在加载Combobox中的一组国家/地区,如果用户选择任何国家/地区,则应在Textbox中显示所选值。我尝试使用属性但我在某处丢失了它。



我的Combobox和TextBox的XAML代码

In my application I'm loading set of countries in Combobox,If user selects any Country the selected value should be displayed in Textbox.I tried with Properties but I lost it somewhere.

My XAML code For Combobox and TextBox

<ComboBox ItemsSource="{Binding Combovalues}" SelectedIndex="{Binding Selectedvalue}" Height="23" HorizontalAlignment="Left" Margin="64,62,0,0" Name="comboBox1" VerticalAlignment="Top" Width="145" />




<TextBox Text="{Binding getvalfromcombo}" Height="23" HorizontalAlignment="Left" Margin="150,134,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />










private ObservableCollection<string> _Combovalues = new ObservableCollection<string>();
        public ObservableCollection<string> Combovalues
        {
            get { return _Combovalues; }
            set
            {
                _Combovalues = value;
                 
            }
                
        }
        private string _Selectedvalue;
        public string Selectedvalue
        {
            get{ return _Selectedvalue; }
            set { _Selectedvalue = value;

            OnNotifyPropertyChanged("Selectedvalue");
            
            }
                
        }

        private string _getvalfromcombo;
        public string getvalfromcombo
        {
            get { return _getvalfromcombo; }
            
            set { _getvalfromcombo = value;
            qwert();
            }
        
        }
        
        public void qwert()
        {
            
            getvalfromcombo = Selectedvalue;

        }


       
        public void comboloadmethod()
        {
            
            TextReader txtreader = new StreamReader(@"D:\ComboSource.xml");
            XmlSerializer xs = new XmlSerializer(typeof(Countrylist));
            n = (Countrylist)xs.Deserialize(txtreader);
            txtreader.Close();
            

        }

        public void combovaluesload()
        {

            if (n != null)
            {
                Combovalues.Add(n.Country1);
                Combovalues.Add(n.Country2);
                Combovalues.Add(n.Country3);
                Combovalues.Add(n.Country4);
            }
        
        }

推荐答案

<combobox itemssource="{Binding Combovalues}" selectedvalue="{Binding getvalfromcombo, Mode=OneWayToSource}" height="23" horizontalalignment="Left" margin="64,62,0,0" name="comboBox1" verticalalignment="Top" width="145" />







<textbox text="{Binding getvalfromcombo, Mode=OneWay}" height="23" horizontalalignment="Left" margin="150,134,0,0" name="textBox3" verticalalignment="Top" width="120" />





这样Combo的SelectedValue和TextBox的Text指向同一个属性。



ComboBox是OneWayToSource为了只设置属性,Text是OneWay只是为了接收属性的值,而不是修改它。





如果你想要,如果你编辑文本的值,组合框也应该改变,你必须设置Mode = T. woWay在两个控件中或只是删除它,因为它默认为TwoWay。



This way the SelectedValue of the Combo and the Text of the TextBox are pointing to the same Property.

The ComboBox is OneWayToSource in order to just set the property, and the Text is OneWay just to recieve the value of the property, not modify it.


If you want that, if you edit the value of the text, the combobox should also change, you must set the Mode=TwoWay in both controls or just delete it, because it is TwoWay by default.


这篇关于检索Combobox Selected项目到Textbox有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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