bool数组,可以从复选框获取其索引的值

时间:2019-06-24 19:40:01

标签: c#

我想制作一个布尔数组,它可以从复选框的状态(checked = trueunchecked = false)中获取其索引的值。因为我想使用该数组更新我的输出。请让我举个例子

<Grid>
    <CheckBox x:Name="Box1" Content="CheckBox1" HorizontalAlignment="Left" Margin="53,62,0,0" VerticalAlignment="Top" />
    <CheckBox x:Name="Box2" Content="CheckBox2" HorizontalAlignment="Left" Margin="53,82,0,0" VerticalAlignment="Top"/>
    <CheckBox x:Name="Box3" Content="CheckBox3" HorizontalAlignment="Left" Margin="53,102,0,0" VerticalAlignment="Top" />
    <CheckBox x:Name="Box4" Content="CheckBox4" HorizontalAlignment="Left" Margin="53,122,0,0" VerticalAlignment="Top" />        
</Grid>

例如,如果我有4个复选框。 Box1和Box3为true。我数组的输出必须为{ true, false, true, false }

1 个答案:

答案 0 :(得分:0)

您可以对复选框进行排列,然后只需选择其选中的值即可:

\n

CheckBox[] chks = { Box1, Box2, Box3, Box4 };
bool[] result = chks.Select(x => x.Checked).ToArray();