简体   繁体   English

UWP C#SQL Web服务GridView触发器

[英]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 :-) 我有带有gridview的XAML代码和连接到SQL Web服务的CS代码,我可以从SQL获取数据:-)

my gridview has 我的GridView有

textblock - data from sql table textblock-SQL表中的数据

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. 我已经将此代码在没有gridviews的其他应用程序中运行,但无法在这里运行。 how do I reference the event_handler inside the gridview 如何在gridview中引用event_handler

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

here is code that works in other app, but this needs to reference the gridview 这是在其他应用程序中可以使用的代码,但这需要引用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,还需要事件CheckBox_Unchecked再次将其隐藏。

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

IsChecked is a nullable type you didnt even checked if it's true. IsChecked是一个可空类型,您甚至没有检查它是否为真。 Your code will hide the txtbox so long as the IsChecked is not null. 只要IsChecked不为null,您的代码将隐藏txtbox。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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