简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM