前台:
<routecommand x:key ="selectallrules">
<windows.commandbindings>
<commandbinding command ="{staticresource selectallrules}" executed="selectallrulescommand_executed"></commandbindings>
</windows.commandbindings>
<checkbox command="{staticresource selectallrules}"></checkbox>
后台:
private void SelectAllRulesCommand_Executed(object sender,ExecutedRoutedEventArgs e)
{
CheckBox checkbox = e.OriginalSource as CheckBox;
bool? value = checkbox.IsChecked;
int row = Grid.GetRow(checkbox);
int column = 1;
for(int i=0;i<this.grid.Children.Count;i++)
{
if(Grid.GetRow(grid.Children[i])==row&&Grid.GetColumn(grid.Children[i])==column)
{
for(int j=0;j<VisualTreeHelper.GetChildrenCount(grid.Children[i] as DependencyObject);j++)
{
Border border = VisualTreeHelper.GetChild(grid.Children[i] as DependencyObject, j) as Border;
DockPanel dockpanel = border.Child as DockPanel;
if((dockpanel.Children[1] as ContentPresenter).Content is Grid)
{
Grid expandergrid = (dockpanel.Children[1] as ContentPresenter).Content as Grid;
foreach(var item in expandergrid.Children)
{
if(item is CheckBox)
{
(item as CheckBox).IsChecked = value;
}
}
}
}
}
}
checkboxselectall.IScheck = UpdataCheckboxALLSelectValue();
}
private void ISSelectRulesCommand_Executed(object sender,ExceptionRoutedEventArgs e)
{
CheckBox checkbox = e.OriginalSource as CheckBox;
Grid gridexpand = checkbox.Parent as Grid;
int numbercheckbox = 0;
int selectedchecknumber = 0;
for(int i=0;i<gridexpand.Children.Count;i++)
{
if(gridexpand.Children[i] is CheckBox)
{
numbercheckbox++;
if((gridexpand.Children[i] as CheckBox).IsChecked ==true)
{
selectedchecknumber++;
}
}
}
Expander expander = gridexpand.Parent as Expander;
int row = Grid.GetRow(expander);
int column = 0;
for(int i=0;i<this.grid.Children.Count;i++)
{
if(Grid.GetRow(grid.Children[i])==row && Grid.GetColumn(grid.Children[i])==column)
{
CheckBox checkboxparent = grid.Children[i] as CheckBox;
if(selectedchecknumber==numbercheckbox)
{
checkboxparent.IsChecked = true;
}
else if(selectedchecknumber ==0)
{
checkboxparent.IsChecked = false;
}
else
{
checkboxparent.IsChecked = null;
}
}
}
checkboxselectall.IScheck = UpdataCheckboxALLSelectValue();
}
private bool? UpdataCheckboxALLSelectValue()
{
bool? result = false;
int numbercheckbox = 0;
int selectchecknumber = 0;
int unselectchecknumber = 0;
for(int i=0;i<this.grid.Children.Count;i++)
{
if(this.grid.Children[i] is CheckBox)
{
numbercheckbox++;
if((this.grid.Children[i]as CheckBox).IsChecked ==true)
{
selectchecknumber++;
}
else if((this.grid.Children[i] as CheckBox).IsChecked ==false)
{
unselectchecknumber++;
}
}
}
if(selectchecknumber ==numbercheckbox)
{
result = true;
}
else if(unselectchecknumber ==numbercheckbox)
{
result = false;
}
else
{
result = null;
}
return result;
}
private void SelectALLCommand_Excuted(object sender, ExceptionRoutedEventArgs e)
{
bool? value = (e.OriginalSource as CheckBox).IsChecked;
if(value ==false)
{
for(int i=0;i<this.grid.Children.Count;i++)
{
if(this.grid.Children[i] is CheckBox)
{
(this.grid.Children[i] as CheckBox).IsChecked = false;
}
else if (this.grid.Children[i] is Expander)
{
Grid gridexpander = (this.grid.Children[i] as Expander).Content as Grid;
foreach(var item in gridexpander.Children)
{
if(item is CheckBox)
{
(item as CheckBox).IsChecked = false;
}
}
}
}
}
else if (value ==true)
{
for (int i = 0; i < this.grid.Children.Count; i++)
{
if (this.grid.Children[i] is CheckBox)
{
(this.grid.Children[i] as CheckBox).IsChecked = true;
}
else if (this.grid.Children[i] is Expander)
{
Grid gridexpander = (this.grid.Children[i] as Expander).Content as Grid;
foreach (var item in gridexpander.Children)
{
if (item is CheckBox)
{
(item as CheckBox).IsChecked = true;
}
}
}
}
}
}