How to update combobox value on changing selection c# dynamically using Binding() in c# (not xaml)

橙三吉。 提交于 2019-12-13 05:04:49

问题


I am c# silverlight5 beginner and i have a situation that i have to created a combo box dynamically using c# and kept items in it. But the problem now when i run it run properly showing the last value by default but when i select the another value it don't update the that value in the text box near by because it is not working dynamically.

I guess i need to add some selection changed or some other event using c#. But i dont know how to do that. Please note that i have created this combo box using c# only.

How to change the value in correspondng selection to the combobox value ?(just using c#)


回答1:


Just when creating the combobox, create also the binding in c#, then it will update the textbox automatically without the need for any events or additional code.

  var binding = new Binding("Text");
  binding.Source = cb;
  binding.StringFormat = "{0} millions";
  txtblk2.SetBinding(TextBlock.TextProperty, binding);



回答2:


At last i have done it using SelectionChanged and creating SelectionChangedEventHandler. The code is as below (may be useful for some future user):

cb.SelectionChanged += new SelectionChangedEventHandler(comboBox1_SelectionChanged); in converter() function and outside converter function :

 void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        txtblk2.Text = cb.SelectedValue.ToString() + " millions";
    }


来源:https://stackoverflow.com/questions/23609074/how-to-update-combobox-value-on-changing-selection-c-sharp-dynamically-using-bin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!