use of combobox to appear another combobox C# - c#

I want to make this as, once i select the first item(Student Information) of the first_ComboBox want to appear second_ComboBox.
How can I make this happen
In the cs code
private void first_ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void second_ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
in XAML
<StackPanel Margin="97,47,171,499" Orientation="Horizontal" Grid.Row="1">
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Where You want to Control" VerticalAlignment="Top" Height="82" Width="463" FontSize="36"/>
<ComboBox x:Name="first_ComboBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="560" Height="42" SelectionChanged="first_ComboBox_SelectionChanged">
<x:String>Student Information</x:String>
<x:String>Staff Information</x:String>
<x:String>Academic Information</x:String>
</ComboBox>
</StackPanel>
<StackPanel Margin="97,172,171,374" Orientation="Horizontal" Grid.Row="1">
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Select the Field" VerticalAlignment="Top" Height="82" Width="463" FontSize="36"/>
<ComboBox x:Name="second_ComboBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="560" Height="42" SelectionChanged="second_ComboBox_SelectionChanged">
<x:String>Student Name</x:String>
<x:String>Student Address</x:String>
</ComboBox>
</StackPanel>

On your Form main, you can set the visibility of Second Combobox to be false, and then on the first combobox selection changed set it to true, Something like this
public MainWindow()
{
InitializeComponent();
second_ComboBox.Visibility = Visibility.Collapsed;
}
private void first_ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selecteditem = first_ComboBox.SelectedItem as string;
if (selecteditem != null)
{
second_ComboBox.Visibility = Visibility.Visible;
}
}

on initialization make second combobox visiblity hidden
public MainWindow()
{
InitializeComponent();
second_ComboBox.Visibility = Visibility.Collapsed;
}
private void first_ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
first_ComboBox.Visibility = System.Windows.Visibility.Visible;
}

Related

UWP C# SQL Webservice GridView triggers

i have class created for data retrieval
I have XAML code with gridview and cs code which connects to SQL webservice and i can get data from SQL :-)
my gridview has
textblock - data from sql table
checkbox
textbox
I would like to have some actions on the checkbox and textboxes. How do I get my textboxes to become visible upon checkbox click?
I have got this code working in other apps without gridviews, but I can't get it to work here. how do I reference the event_handler inside the gridview
XAML example
<GridView x:Name="GreenQuestionGridView" ItemsSource="{Binding}" Background="Green" Margin="0,40,0,0">
<GridView.ItemTemplate>
<DataTemplate>
<Grid Height="40" Width="600" >
<StackPanel Orientation="Horizontal">
<TextBlock Width="200" VerticalAlignment="Bottom" TextWrapping="Wrap" Text="{Binding question_green}" />
<CheckBox x:Name="chkBox" Checked="chkBox_Checked" Unchecked="chkBox_Unchecked" Indeterminate="chkBox_Indeterminate" VerticalAlignment="Bottom" IsThreeState="True" />
<TextBox x:Name="txtBox" Visibility="Collapsed" Width="200" VerticalAlignment="Bottom" />
</StackPanel>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
here is code that works in other app, but this needs to reference the gridview
private void chkBox_Checked(object sender, RoutedEventArgs e)
{
if (chkbox.IsChecked == null)
{
txtbox.Visibility = Visibility.Visible;
}
else
{
txtbox.Visibility = Visibility.Collapsed;
}
}
For the CheckBox you need also the event CheckBox_Unchecked to hide it again.
<CheckBox Unchecked="CheckBox_Unchecked" Checked="CheckBox_Checked" ... />
IsChecked is a nullable type you didnt even checked if it's true. Your code will hide the txtbox so long as the IsChecked is not null.
private static void ToggleTextBoxVisibility(object sender) {
if(!(sender is CheckBox)) {
return;
}
CheckBox checkBox = sender as CheckBox;
foreach(var child in ((checkBox.Parent as StackPanel).Children)) {
if(!(child is TextBox)) {
continue;
}
TextBox textBox = child as TextBox;
if(checkBox.IsChecked.HasValue && checkBox.IsChecked.Value) {
textBox.Visibility = Visibility.Visible;
} else {
textBox.Visibility = Visibility.Collapsed;
}
}
}
private void CheckBox_Checked(object sender, RoutedEventArgs e) {
ToggleTextBoxVisibility(sender);
}
private void CheckBox_Unchecked(object sender, RoutedEventArgs e) {
ToggleTextBoxVisibility(sender);
}
A clean solution would be to control it with a binding to a property in your viewmodel.

How to get text from TextBox inside ListViewItem's DataTemplate

I don't know how to get text from "firstBox" and "secondBox" after button click.
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<!-- some code -->
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Data}" VerticalAlignment="Top" Height="18" Width="100" FontSize="13.333" Margin="162,9,0,0"/>
<TextBlock HorizontalAlignment="Left" Margin="0,35,0,0" TextWrapping="Wrap" Text="{Binding D_gospodarzy}" FontSize="14.667" VerticalAlignment="Top" Height="59" Width="100"/>
<TextBlock HorizontalAlignment="Center" Margin="268,35,7,0" TextWrapping="Wrap" Text="{Binding D_gosci}" FontSize="14.667" VerticalAlignment="Top" Width="100" Height="59"/>
<TextBox x:Name="firstBox" ... />
<Button Content="Click" " Click="Button_Click_1"/>
<TextBox x:Name="secondBox" ... />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
I get only the object
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var myobject = (sender as Button).DataContext;
}
There are cuple of ways to do it, for example you can traverse the VisualTree of clicked button's parent and retrive TextBox with the name you want. In this case, I would take advantage of an extension method written by yasen in this answer.
Then it can look for example like this:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var parent = (sender as Button).Parent;
TextBox firstOne = parent.GetChildrenOfType<TextBox>().First(x => x.Name == "firstBox");
Debug.WriteLine(firstOne.Text);
}
Remember to put an extension method somewhere in a static class:
public static class Extensions
{
public static IEnumerable<T> GetChildrenOfType<T>(this DependencyObject start) where T : class
{
// rest of the code
Here's how to get the text..
String text1 = firstBox.Text;
String text2 = secondBox.Text;
note: firstBox and secondBox must be class members to use them in different class methods.

selecteditem or selectedindex for listpicker

When App opens, selectedItem of the ListPicker namely, "BackgroundColor" must be from variable. How to achieve this?
XAML:
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Name="PickerItemTemplate">
<TextBlock Text="{Binding BackGroundColorString}" />
</DataTemplate>
<DataTemplate x:Name="PickerFullModeItemTemplate" >
<Grid x:Name="rootGrid" Margin="0">
<StackPanel Orientation="Horizontal" Margin="0 14 0 0" HorizontalAlignment="Center">
<TextBlock Name="BackgroundColor"
Text="{Binding BackGroundColorString}"
FontSize="35"
Margin="10,10"
TextAlignment="Center"
FontFamily="/Assets/Fonts/AGENCYR.TTF#Agency FB"
/>
</StackPanel>
</Grid>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<toolkit:ListPicker x:Name="BackgroundColor" FullModeHeader="Select Background Color:"
Header="Background Color:" BorderThickness="0"
FullModeItemTemplate="{StaticResource PickerFullModeItemTemplate}"
ItemTemplate="{StaticResource PickerItemTemplate}" Background="#FF09043C"
SelectionChanged="BackgroundColor_SelectionChanged" >
</toolkit:ListPicker>
C#:
public class BackGroundlistPickerClass
{
public string BackGroundColorString
{
get;
set;
}
}
List<BackGroundlistPickerClass> BackGroundColorList = new List<BackGroundlistPickerClass>();
public void ImplementListPickeritems() //Listpickers
{
BackGroundColorList.Add(new BackGroundlistPickerClass() { BackGroundColorString = "White (Default)" });
BackGroundColorList.Add(new BackGroundlistPickerClass() { BackGroundColorString = "Black" });
BackGroundColorList.Add(new BackGroundlistPickerClass() { BackGroundColorString = "Light Grey" });
}
string PreSelectedColor="Black";
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
BackgroundColor.SelectedItem=PreSelectedColor; // ERROR COMES ON THIS LINE
}
BackgroundColor.SelectedItem is not working because, items in BackgroundColor are acutaly from Class/List. Now how to set BackgroundColor listpicker to Black(PreSelectedColor) whenever the page opens?
You need to set SelectedItem to an item from the ItemsSource. You can try this way, assuming that BackGroundColorList property used for ItemsSource :
string PreSelectedColor="Black";
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
var defaultColor =
BackGroundColorList.FirstOrDefault(o => o.BackGroundColorString == PreSelectedColor);
BackgroundColor.SelectedItem = defaultColor;
}

How can select a item from listbox binding Windows Phone 7

I can't get a text from listbox with SELECTEDITEM event but it doesn't and only return a string like "Li.Medicamento".
My code xaml:
<ListBox x:Name="list_enfer" Margin="38,210,40,65" Visibility="Collapsed">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding enfermedadasoc}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
My code in C# is the next:
private void visualizar_est_Click(object sender, System.EventArgs e)
{
MessageBox.Show(list_enfer.SelectedItem.ToString());
}
private void visualizar_est_Click(object sender, System.EventArgs e)
{
var mySelectedItem = list_enfer.SelectedItem as yourObject;
//Then you can access the property inside yourObject
MessageBox.Show(yourObject.enfermedadasoc.ToString());
}

how to set values passing one page to another page?

Hi this is am using xaml page,i want to navigate with selected item.
<ListBox x:Name="NotchsList11" Margin="0,0,0,0" Grid.Row="3" HorizontalAlignment="left" Width="Auto" Grid.RowSpan="2">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel VerticalAlignment="Top" Width="Auto">
<ListBox ItemsSource="{Binding Images}" Width="Auto" ScrollViewer.VerticalScrollBarVisibility="Disabled" SelectionChanged="NotchsList11_SelectionChanged">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
</StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}" Width="152" Height="90" Stretch="Fill" VerticalAlignment="Top">
</Image>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
My xaml.cs page code is given below
private void NotchsList11_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Notch selectedItemData = (sender as ListBox).SelectedItem as Notch;
if(selectedItemData != null)
{
NavigationService.Navigate(new Uri(string.Format("/Test.xaml?parameter={0}",selectedItemData.articleid), UriKind.Relative));
}
}
if i select any one item i cant navigate to other page,because i got error selectedItemData is null.so any one can help me?
Don't try to pull from the sender object. Just pull what you need directly from the list object if in fact the code believes that an item is truly selected and not null.
You just need to change your SelectionChanged method to look like this:
private void NotchsList11_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (NotchsList11.SelectedItem != null)
{
NavigationService.Navigate(new Uri(string.Format("/Test.xaml?parameter={0}",
(NotchsList11.SelectedItem as Notch).articleid), UriKind.Relative));
}
}
Here's an example:
http://code.msdn.microsoft.com/wpapps/Windows-Phone-8-JumpStart-1b7c34e3/sourcecode?fileId=72994&pathId=1694015380
A.
private void NotchsList11_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Notch selectedItemData = NotchsList11.SelectedItem as Notch;
if (selectedItemData != null)
{
NavigationService.Navigate(new Uri(string.Format("/Test.xaml?parameter={0}",selectedItemData.articleid), UriKind.Relative));
}
}
B. Or you can use whole SelectedItemData object as data context
private void NotchsList11_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Notch selectedItemData = NotchsList11.SelectedItem as Notch;
if (selectedItemData != null)
{
NavigationService.Navigate(new Uri("/Test.xaml", UriKind.Relative));
FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
root.DataContext = selectedItemData;
}
}
Remarks: In Test page you can use (Notch)DataContext to reference instance of the Class so you can use Image data as ((Notch)DataContext).Image
C. Or, this way:
private void NotchsList11_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
NavigationService.Navigate(new Uri(((Notch)e.AddedItems[0]).articleid, UriKind.Relative));
((ListBox)sender).SelectedIndex = -1;
Remarks: Notch is class that you Bind as ItemSource to ListBox
Best regards
Spaso

Categories