Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to retrieve selected Combo Box item in User Control Page
Combo Box Control is in Main Window.Upon Selection User Control will be loaded with selected values
how to achieve this?
Posted

You can try this. Not tested
C#
Window parentWindow = Window.GetWindow(this);
ComboBox cmb = parentWindow.Controls["ComboBoxID"] as ComboBox;

Now You can get the value
C#
string val = cmb.Text;

OR
C#
ComboBoxItem cmbItem= cmb.SelectedItem as ComboBoxItem;
string val = cmbItem.Content.ToString();
 
Share this answer
 
v4
Comments
mithun286 17-Nov-15 0:53am    
This are the errors i am getting:


Error 2 'WpfApplication1.WindowDropDownMap' does not contain a definition for 'Controls' and no extension method 'Controls' accepting a first argument of type 'WpfApplication1.WindowDropDownMap' could be found (are you missing a using directive or an assembly reference?) C:\Users\Mithun\Downloads\wpfApplication\WpfApplication1\WpfApplication1\UserControlMap.xaml.cs 43 39 WpfApplication1


Error 3 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?) C:\Users\Mithun\Downloads\wpfApplication\WpfApplication1\WpfApplication1\UserControlMap.xaml.cs 44 26 WpfApplication1
[no name] 17-Nov-15 1:04am    
Try now. Updated the solution according to your application.
mithun286 17-Nov-15 0:59am    
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
WindowDropDownMap control = new WindowDropDownMap();

WindowDropDownMap parentForm = this.Parent as WindowDropDownMap;
ComboBox cmb = parentForm.Controls["selectOption"] as ComboBox;
string val =cmb.SelectedValue;
}

This is the way i used your code
please correct if i am wrong
[no name] 17-Nov-15 1:03am    
Use this code in your UserControl_Loaded event
Window parentWindow = Window.GetWindow(this);
ComboBox cmb = parentWindow.Controls["ComboBoxID"] as ComboBox;
string val = Convert.ToString(cmb.SelectedValue);
mithun286 17-Nov-15 1:18am    
My Window name is
WindowDropDownMap
and i am getting error near Controls
Do not have the definition for Controls
Write this in your WindowDropDownMap class
C#
public static string CMBSelectedValue {get;set;}

On SelectedIndexChanged event assign value
C#
WindowDropDownMap.CMBSelectedValue = ((ComboBoxItem)selectOption.SelectedItem).Content.ToString();

Now trying to get value in User Control like
C#
string str = WindowDropDownMap.CMBSelectedValue;
 
Share this answer
 
XML
<ComboBox HorizontalAlignment="Left" Margin="169,30,0,0" VerticalAlignment="Top" Width="201" Name="selectOption" SelectionChanged="ComboBox_SelectionChanged">
         <ComboBoxItem Content="13.017063"/>
         <ComboBoxItem Content="13.017564"/>
         <ComboBoxItem Content="12.989084"/>
         <ComboBoxItem Content="12.989313"/>
     </ComboBox>

Drop Down Code
 
Share this answer
 

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