Answered by:
I'm not getting control under hub section

Question
-
Hi,
How to get controls of combobox item whether checked or not in C#.
here is an XAML code for which i have created for combobox.<Hub> <HubSection> <DataTemplate> <StackPanel> <ComboBox x:Name="myComboBox"> <ComboBoxItem Content="Male"/> <ComboBoxItem Content="Female"/> <ComboBoxItem Content="other"/> </ComboBox> <Button x:Name="nextButton" Content="Next"/> </StackPanel> </DataTemplate> </HubSection> </Hub>
and code behind it
private void nextButton_Click(object sender, RoutedEventArgs e) { if(myComboBox.isSelected = male)
{
\\TODOD } else if(myComboBox.isSelected = female)
{
\\TODOD } else if(myComboBox.isSelected = other)
{
\\TODOD }
But Im Getting error. Please help me
Saturday, July 26, 2014 11:10 PM
Answers
-
Hi Ashiq,
you cannot access elements via x:Name in the codebehind-file if they are part of a DataTemplate.
What you can do:
Option 1 => Create a UserControl containing your ComboBox and Button and do all the logic in that UserControl. There you don't have a DataTemplate. After you've finished your UserControl, you can use it in a HubSection.
Option 2 => Create a Loaded-EventHandler for the ComboBox and store the ComboBox in a field of your page:
<ComboBox x:Name="myComboBox" Loaded="myComboBox_Loaded">
ComboBox myComboBox; private void myComboBox_Loaded(object sender, RoutedEventArgs e) { myComboBox = (ComboBox)sender; }
Option 3 => Add an EventHandler for the SelectionChanged-Event of the ComboBox. Store the selected item in a field and access it in your next button click handler.
Option 4 => Use MVVM and set a DataContext for your HubSection. Then you can do everything via data binding.
Here a thread with a similar issue and its solution:
Thomas Claudius Huber
"If you can't make your app run faster, make it at least look & feel extremly fast"
My latest Pluralsight-course: Windows Store Apps - Data Binding in Depth
twitter: @thomasclaudiush
homepage: www.thomasclaudiushuber.com
author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook- Proposed as answer by Dave SmitsMVP Sunday, July 27, 2014 10:03 AM
- Marked as answer by Ashiq Hassan Sunday, July 27, 2014 2:19 PM
Sunday, July 27, 2014 7:32 AM
All replies
-
Hi Ashiq,
you cannot access elements via x:Name in the codebehind-file if they are part of a DataTemplate.
What you can do:
Option 1 => Create a UserControl containing your ComboBox and Button and do all the logic in that UserControl. There you don't have a DataTemplate. After you've finished your UserControl, you can use it in a HubSection.
Option 2 => Create a Loaded-EventHandler for the ComboBox and store the ComboBox in a field of your page:
<ComboBox x:Name="myComboBox" Loaded="myComboBox_Loaded">
ComboBox myComboBox; private void myComboBox_Loaded(object sender, RoutedEventArgs e) { myComboBox = (ComboBox)sender; }
Option 3 => Add an EventHandler for the SelectionChanged-Event of the ComboBox. Store the selected item in a field and access it in your next button click handler.
Option 4 => Use MVVM and set a DataContext for your HubSection. Then you can do everything via data binding.
Here a thread with a similar issue and its solution:
Thomas Claudius Huber
"If you can't make your app run faster, make it at least look & feel extremly fast"
My latest Pluralsight-course: Windows Store Apps - Data Binding in Depth
twitter: @thomasclaudiush
homepage: www.thomasclaudiushuber.com
author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook- Proposed as answer by Dave SmitsMVP Sunday, July 27, 2014 10:03 AM
- Marked as answer by Ashiq Hassan Sunday, July 27, 2014 2:19 PM
Sunday, July 27, 2014 7:32 AM -
Thanks a lotSunday, July 27, 2014 2:19 PM