1

我在网格中有许多复选框,我必须检查是否选中了任何复选框,然后它将打印名称,例如 - MsgBox(“CheckboxName”)。

<Grid Height="179" Width="290" Name="Checksum_Collection" >
        <CheckBox Content="Alder32" Height="20" Name="Alder32CheckBox" />
        <CheckBox Content="BsdChecksum" Height="20" Name="BsdChecksumCheckBox"/> 
        <CheckBox Content="CRC16" Height="20" Name="CRC16CheckBox"/>
        <CheckBox Content="Damm" Height="20" Name="DammCheckBox"/>
        <CheckBox Content="Fletcher" Height="20" Name="FletcherCheckBox" />
        <CheckBox Content="LRC" Height="20" Name="LRCCheckBox"  />
        <CheckBox Content="Luhn" Height="20" Name="LuhnCheckBox"/>
        <CheckBox Content="MD5 Hash" Height="20" Name="MD5CheckBox"/>
</Grid> 
4

4 回答 4

3
using System.Linq;

foreach (var checkBox in Checksum_Collection.Children
    .OfType<CheckBox>()
    .Where(cb => (bool)cb.IsChecked))
{
    var name = checkBox.Name;
    Trace.TraceInformation("{0} is checked", name);
}
于 2013-08-09T07:35:12.143 回答
0

试试这个 :

foreach(var child in Checksum_Collection.Children)
{
    if(child.getType() == typeof(CheckBox))
    {
        if(child.IsChecked)
            MsgBox ("CheckboxName");    // or do whatever u want
    }
}
于 2013-08-09T07:34:23.780 回答
0

您可以使用附加到复选框的选中和未选中事件,并在这些事件中从哈希表等集合中添加/删除选定/未选中的项目

<CheckBox Content="Item 1" Checked="CheckBox_Checked"    Unchecked="CheckBox_Unchecked"/>

并在代码中:

List<string> SelectedItems = new List<string>();
    private void CheckBox_Checked(object sender, RoutedEventArgs e)
    {
        SelectedItems.Add((e.OriginalSource as CheckBox).Name.ToString());
    }
    private void CheckBox_UnChecked(object sender, RoutedEventArgs e)
    {
        SelectedItems.Remove((e.OriginalSource as CheckBox).Name.ToString());
    }
于 2015-05-10T08:23:00.970 回答
0

XAML

<dxe:CheckEdit IsChecked="{Binding RowData.Row.IsSelected}" Checked="CheckEdit_Checked" 
 Unchecked="CheckEdit_Checked"  HorizontalAlignment="Center" VerticalAlignment="Center"/>

代码背后

有一个类级别:isAnyChecked = 0;

private void CheckEdit_Checked(object sender, RoutedEventArgs e)
{
if (e.RoutedEvent.Name == "Checked")
{
    isAnyChecked += 1;
}
else if(e.RoutedEvent.Name =="Unchecked")
{
    isAnyChecked -= 1;
    if(isAnyChecked == 0)
}
}
于 2016-08-18T20:14:04.317 回答