UWP C# SQL Webservice GridView triggers

本文关键字:GridView triggers Webservice SQL UWP | 更新日期: 2023-09-27 18:05:54

我已经为数据检索创建了一个类

我有XAML代码与gridview和cs代码连接到SQL webservice,我可以从SQL中获取数据:-)

my gridview has

textblock - data from SQL table

复选框文本框

我想对复选框和文本框进行一些操作。我如何让我的文本框成为可见的复选框点击?

我有这个代码在其他应用程序没有gridviews工作,但我不能让它在这里工作。我如何在gridview中引用event_handler

XAML例子

<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>

这是在其他应用程序中工作的代码,但这需要引用gridview

private void chkBox_Checked(object sender, RoutedEventArgs e)
     {         
         if (chkbox.IsChecked == null)
         {
             txtbox.Visibility = Visibility.Visible;
         }
         else
         {
             txtbox.Visibility = Visibility.Collapsed;
         }
     }

UWP C# SQL Webservice GridView triggers

对于CheckBox,您还需要事件CheckBox_Unchecked来再次隐藏它。

<CheckBox Unchecked="CheckBox_Unchecked" Checked="CheckBox_Checked" ... />

IsChecked是一个可空类型,你甚至没有检查它是否为真。你的代码将隐藏文本框,只要IsChecked不为空。

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);
}
一个干净的解决方案是通过绑定到视图模型中的属性来控制它。