如何在c#

时间:2017-08-01 07:45:52

标签: c# xaml windows-phone-8.1

如何在c#中单击Select_All按钮时检查列表框中的所有复选框。

这是我的XAML

<StackPanel Width="400" Orientation="Horizontal" Grid.Row="0">
    <AppBarButton x:Name="Btn_SelectAll" Margin="20,0,0,0" Label="Select All" Icon="SelectAll" Click="Btn_SelectAll_Click" FontWeight="Bold" />
    <AppBarButton x:Name="Btn_Delete" Margin="150,0,0,0" Label="Delete All" Icon="Delete" Click="DeleteAll_Click" FontWeight="Bold" />
</StackPanel>
<ListBox  x:Name="listBoxobj" Background="Transparent" Margin="6" Height="auto" BorderThickness="2" MaxHeight="580" Grid.Row="1" SelectionChanged="listBoxobj_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Name="Option1CheckBox"  VerticalAlignment="Center" Margin="5,0,0,0" />
            <TextBlock x:Name="NameTxt" TextWrapping="Wrap" Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Left" FontSize="22" Foreground="White"/>
            <TextBlock x:Name="Age"   TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="White" FontSize="22" Text="{Binding Age}" />
        </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

列表框中的数据是从数据库中填充的,因此当用户点击SelectAll时,应检查所有复选框。

1 个答案:

答案 0 :(得分:0)

在CodeBehind中创建一个公共/受保护的字段,如

protected bool SelectAll = false;

Btn_SelectAll_Click eventhandler中,设置SelectAll = true;

使用SelectAll字段绑定Option1CheckBox IsChecked属性,如下所示:

<CheckBox Name="Option1CheckBox"  VerticalAlignment="Center" Margin="5,0,0,0" IsChecked="{Binding SelectAll}" />