本文介绍了限制基于Combobox的CheckBoxes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下午好
我试图限制复选框的数量而不是可以勾选,取决于组合框值(组合中的1表示可以勾选1个框等) )。我不知道该怎么做。请有人帮忙。
I am trying to limit the amount of checkboxes than can be ticked, depening ont he combobox value (1 in the combo means 1 box can be ticked etc). I am not sure how to go about it. Please can somebody help.
谢谢
<CheckBox Content="CB1" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Grid.Row="1" Width="53" IsChecked="False" Checked="CheckBoxChecked" Unchecked="CheckBoxUnchecked"/>
<CheckBox Content="CB2" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Grid.Row="1" Width="53" IsChecked="False" />
<CheckBox Content="CB3" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Grid.Row="1" Width="53" IsChecked="False" />
<CheckBox Content="CB4" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Grid.Row="1" Width="53" IsChecked="False" />
<Label x:Name="TotalofCheckBoxes" Content="Total" HorizontalAlignment="Left" VerticalAlignment="Top" Height="38" Width="99" FontSize="20" Grid.Row="1" />
<ComboBox Name="Total" HorizontalAlignment="Left" Margin="549,181,0,0" VerticalAlignment="Top" Width="231" Height="40" FontSize="20" Background="#FF1E2A7F" OpacityMask="White" Foreground="Red" Grid.Row="1">
<ComboBoxItem Content="0" />
<ComboBoxItem Content="1" />
<ComboBoxItem Content="2" />
<ComboBoxItem Content="3" />
<ComboBoxItem Content="4" />
<ComboBoxItem Content="5" />
</ComboBox>
$
推荐答案
<Grid>
<StackPanel>
<CheckBox Content="CB1" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Grid.Row="1" Width="53" IsChecked="False" />
<CheckBox Content="CB2" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Grid.Row="1" Width="53" IsChecked="False" />
<CheckBox Content="CB3" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Grid.Row="1" Width="53" IsChecked="False" />
<CheckBox Content="CB4" HorizontalAlignment="Left" VerticalAlignment="Top" Height="18" Grid.Row="1" Width="53" IsChecked="False" />
<Label x:Name="TotalofCheckBoxes" Content="Total" HorizontalAlignment="Left" VerticalAlignment="Top" Height="38" Width="99" FontSize="20" Grid.Row="1" />
<ComboBox Name="Total" HorizontalAlignment="Left" VerticalAlignment="Top" Width="231" Height="40" FontSize="20" Background="#FF1E2A7F" OpacityMask="White" Foreground="Red" Grid.Row="1" SelectionChanged="Total_SelectionChanged">
<ComboBoxItem Content="0" />
<ComboBoxItem Content="1" />
<ComboBoxItem Content="2" />
<ComboBoxItem Content="3" />
<ComboBoxItem Content="4" />
<ComboBoxItem Content="5" />
</ComboBox>
</StackPanel>
</Grid>
private void Total_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// string text = (e.AddedItems[0] as ComboBoxItem).Content as string;
string text = ((sender as ComboBox).SelectedItem as ComboBoxItem).Content as string;
if (text == null || text == "")
{
return;
}
int countcb = Convert.ToInt32(text);
foreach (CheckBox cb in FindVisualChildren<CheckBox>(this))
{
cb.IsEnabled = true;
cb.IsChecked = false;
}
foreach (CheckBox cb in FindVisualChildren<CheckBox>(this))
{
int indexi = Convert.ToInt32(cb.Content.ToString().Substring(2));
if (indexi > countcb)
{
cb.IsEnabled = false;
cb.IsChecked = false;
}
}
}
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
这篇关于限制基于Combobox的CheckBoxes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!