question

Jramos avatar image
0 Votes"
Jramos asked AryaDing-MSFT edited

how to hide make Visible Checkbox through Button

I want to do is from the select button can make visible or collapsed the check boxes that are in the ListView lel and then select to be able to delete the record from the table.

 ListView x:Name="ListPet"
                           SelectionMode="single"
                           ScrollViewer.VerticalScrollBarVisibility="Auto"
                           ScrollViewer.IsVerticalRailEnabled="True"
                           ScrollViewer.VerticalScrollMode="Enabled"
                              
                           Margin="20">
                     <ListView.HeaderTemplate>
                         <DataTemplate >
                             <Grid  Padding="12" >
                                 <Grid.ColumnDefinitions>
    
                                        
                                     <ColumnDefinition Width="60"/>
                                     <ColumnDefinition Width="100"/>
                                     <ColumnDefinition Width="190"/>
                                     <ColumnDefinition Width="100"/>
                                     <ColumnDefinition Width="100"/>
                                     <ColumnDefinition Width="90"/>
                                     <ColumnDefinition  Width="90"/>
                                     <ColumnDefinition Width="90"/>
                                     <ColumnDefinition Width="110"/>
                                 </Grid.ColumnDefinitions>
    
    
                                    
                                 <TextBlock x:Uid="Foto" Grid.Column="1" VerticalAlignment="center" Foreground="Gray" FontWeight="SemiBold" FontSize="{StaticResource SmallFontSize}"/>
                                 <TextBlock x:Uid="Tipo_mascota"  Grid.Column="2" VerticalAlignment="Center"  Foreground="Gray" FontWeight="SemiBold" FontSize="{StaticResource SmallFontSize}"/>
                                 <TextBlock x:Uid="Fecha_Nac" x:Name="txtFecha"  Grid.Column="3" VerticalAlignment="Center"   Foreground="Gray"  FontWeight="SemiBold" FontSize="{StaticResource SmallFontSize}"/>
                                 <TextBlock x:Uid="Nombre"  VerticalAlignment="Center"   Grid.Column="4" Foreground="Gray"  FontWeight="SemiBold" FontSize="{StaticResource SmallFontSize}"/>
                                 <TextBlock x:Uid="Raza"  VerticalAlignment="Center"  Grid.Column="5" Foreground="Gray"  FontWeight="SemiBold" FontSize="{StaticResource SmallFontSize}"/>
                                 <TextBlock x:Uid="Color"  VerticalAlignment="Center" Grid.Column="6" Foreground="Gray"  FontWeight="SemiBold" FontSize="{StaticResource SmallFontSize}"/>
                                 <TextBlock x:Uid="Chip"  VerticalAlignment="Center" Grid.Column="7" Foreground="Gray"  FontWeight="SemiBold" FontSize="{StaticResource SmallFontSize}"/>
                                 <TextBlock x:Uid="Genero"  VerticalAlignment="Center" Grid.Column="8" Foreground="Gray"  FontWeight="SemiBold" FontSize="{StaticResource SmallFontSize}"/>
                                 <TextBlock x:Uid="Esterilizado"  VerticalAlignment="Center" Grid.Column="9"  Foreground="Gray"  FontWeight="SemiBold" FontSize="{StaticResource SmallFontSize}"/>
    
                                   
    
                             </Grid>
    
                               
                         </DataTemplate>
                     </ListView.HeaderTemplate>
                     <ListView.ItemTemplate>
                         <DataTemplate x:DataType="local:PetTable">
                             <Grid Height="48">
                                 <Grid.ColumnDefinitions>
                                     <ColumnDefinition Width="32"/>
                                     <ColumnDefinition Width="32"/>
                                     <ColumnDefinition Width="10"/>
                                     <ColumnDefinition Width="100"/>
                                     <ColumnDefinition Width="200"/>
                                     <ColumnDefinition Width="160"/>
                                     <ColumnDefinition Width="160"/>
                                     <ColumnDefinition Width="90"/>
                                     <ColumnDefinition  Width="90"/>
                                     <ColumnDefinition Width="90"/>
                                     <ColumnDefinition Width="110"/>
                                 </Grid.ColumnDefinitions>
    
                                 <CheckBox x:Name="Ch1" Grid.Column="0" Checked="CheckBox1_Checked" Unchecked="CheckBox1_UnChecked"  />
                                 <Ellipse Height="32" Width="32" Grid.Column="1" VerticalAlignment="center">
                                     <Ellipse.Fill>
                                         <ImageBrush />
                                     </Ellipse.Fill>
                                 </Ellipse>
    
                                    
                                   
                                 <TextBlock   Text="{x:Bind Type}" Grid.Column="2"  VerticalAlignment="center" FontSize="{StaticResource SmallFontSize}"/>
                                 <TextBlock   Text="{x:Bind DuoDate.ToShortDateString()}" Grid.Column="3" VerticalAlignment="center"  FontSize="{StaticResource SmallFontSize}"/>
                                 <TextBlock   Text="{x:Bind PetName }" Grid.Column="4" VerticalAlignment="center" FontSize="{StaticResource SmallFontSize}"/>
                                 <TextBlock   Text="{x:Bind PetRace}"  Grid.Column="5" VerticalAlignment="center" FontSize="{StaticResource SmallFontSize}"/>
                                 <TextBlock   Text="{x:Bind PetColor}"  Grid.Column="6" VerticalAlignment="center" FontSize="{StaticResource SmallFontSize}"/>
                                 <TextBlock   Text="{x:Bind PetChip}"  Grid.Column="7" VerticalAlignment="center" FontSize="{StaticResource SmallFontSize}"/>
                                 <TextBlock   Text="{x:Bind PetGender}"  Grid.Column="8" VerticalAlignment="center" FontSize="{StaticResource SmallFontSize}"/>
                                 <TextBlock   Text="{x:Bind PetSterile}"  Grid.Column="9"  VerticalAlignment="center" FontSize="{StaticResource SmallFontSize}"/>
                             </Grid>
                         </DataTemplate>
                     </ListView.ItemTemplate>
                        
                 </ListView>
windows-uwp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Jramos Has your question been solved? If yes, you could accept useful answer, which will help others that face the same question.

0 Votes 0 ·

1 Answer

AryaDing-MSFT avatar image
0 Votes"
AryaDing-MSFT answered Jramos commented

Hi,

Welcome to Microsoft Q&A!

For checkbox Visibility, you could try to bind the checkbox Visibility to a public Property named Sign, then you could use button to control the change of Sign in code behind. Besides, if you want to remove the current ListViewItem when the checkbox is selected, you could get the DataContext of this checkbox and convert a corresponding object, then remove this object from the binding collection. Please refer to the following code.

Xaml code:

    <Grid>
         <StackPanel>
             <ListView x:Name="ListPet" ItemsSource="{x:Bind persons,Mode=OneWay}" >
                 <ListView.ItemTemplate>
                 <DataTemplate x:DataType="local:Person">
                     <Grid>
                         <Grid.ColumnDefinitions>
                             <ColumnDefinition Width="32"/>
                             <ColumnDefinition Width="35"/>
                                 <ColumnDefinition Width="35"/>
                             </Grid.ColumnDefinitions>
                             <CheckBox x:Name="ch1" Visibility="{x:Bind Sign,Mode=OneWay}" Grid.Column="0" Checked="CheckBox1_Checked"  />
                         <TextBlock Text="{x:Bind Name}" Grid.Column="1"  VerticalAlignment="center" />
                             <TextBlock Text="{x:Bind Job}" Grid.Column="2"  VerticalAlignment="center" />
                         </Grid>
                       
                 </DataTemplate>
             </ListView.ItemTemplate>
         </ListView>
             <Button x:Name="CollapsedButton" Content="Collapsed" Click="CollapsedButton_Click"/>
             <Button x:Name="VisibleButton" Content="Visible" Click="VisibleButton_Click"/>
         </StackPanel>
     </Grid>

Code behind:

 public sealed partial class MainPage : Page
 {
   
 public ObservableCollection<Person> persons;
 public MainPage()
 {
     this.InitializeComponent();
     persons = new ObservableCollection<Person>() {
         new Person(){Name="tom",Job="teacher"},
         new Person(){Name="ming",Job="lawyer"},
         new Person(){Name="kiki",Job="nurser"},
         new Person(){Name="cc",Job="teacher"}
     };
        
 }

 

 private void colButton_Click(object sender, RoutedEventArgs e)
 {
     foreach (var p in persons)
     {
         p.Sign = true;
     }
 }

 private void CheckBox1_Checked(object sender, RoutedEventArgs e)
 {
     CheckBox checkBox=sender as CheckBox;
     Person p= checkBox.DataContext as Person;
     persons.Remove(p);
       
 }

 private void VisibleButton_Click(object sender, RoutedEventArgs e)
 {
     foreach (var p in persons)
     {
         p.Sign = true;
     }
 }

 private void CollapsedButton_Click(object sender, RoutedEventArgs e)
 {
     foreach (var p in persons)
     {
         p.Sign = false;
      }
   }
  }

 public class Person: INotifyPropertyChanged
 {
  
 public string Name { get; set; }
 public string Job { get; set; }
   
 private Boolean sign;
 public Boolean Sign
 {
     get { return sign; }
     set
     {
         sign = value;
         RaisepropertyChanged("Sign");
     }
 }
 public Person()
 {
   this.Sign = true;
 }
 public event PropertyChangedEventHandler PropertyChanged;
 public void RaisepropertyChanged(string propertyname=null) {
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
  }
 }


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.




· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

can be done in the same button the visible and collapsed

0 Votes 0 ·

@Jramos You could try to use ToggleSwitch control to switch the visbility of the checkbox. Or for a normal button, you could get the current visibility of checkbox then change it to its opposite. As follows:

 private void ChangeVisbilityButton_Click(object sender, RoutedEventArgs e)
         {
            var sign= persons.Select(p => p.Sign).FirstOrDefault();
             foreach (var p in persons)
             {
                 p.Sign = !sign;
             }
         }


0 Votes 0 ·

what the app does is put data into the database

0 Votes 0 ·