Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
XML
<ComboBox ItemsSource="{Binding Combovalues}" SelectedIndex="{Binding Selectedvalue}" Height="23" HorizontalAlignment="Left" Margin="64,62,0,0" Name="comboBox1" VerticalAlignment="Top" Width="145" />

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



C#
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);
            }
        
        }
Posted

1 solution

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


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.
 
Share this answer
 
Comments
WPF_Mvvm 13-May-14 6:31am    
Thanks Gerard !!

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