0
我有多個DataGrids第一列有一個複選框,並且標題也有一個複選框。正如我們都看到的,我希望能夠點擊標題複選框並讓它選中下面的所有複選框。WPF DataGrid - 在運行時修改視圖滾動視圖
<DataGrid x:Name="dgReportTypes_New" Margin="10,140,0,0" Height="175" VerticalAlignment="Top" HorizontalAlignment="Left" Width="216" AutoGenerateColumns="False" SelectionMode="Single" CanUserAddRows="False" MouseUp="DataGrid_MouseUp" KeyDown="DataGrid_KeyDown">
<DataGrid.Columns>
<DataGridCheckBoxColumn IsReadOnly="True" Binding="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}" ClipboardContentBinding="{x:Null}">
<DataGridCheckBoxColumn.HeaderTemplate >
<DataTemplate>
<CheckBox x:Name="chkSelectAll" IsThreeState="True" Checked="DataGridHeader_Checked" Unchecked="DataGridHeader_Unchecked" Indeterminate="DataGridHeader_Indeterminate" />
</DataTemplate>
</DataGridCheckBoxColumn.HeaderTemplate>
</DataGridCheckBoxColumn>
<DataGridTextColumn IsReadOnly="True" Binding="{Binding Description}" CanUserResize="False" Width="*" />
</DataGrid.Columns>
</DataGrid>
經過
private void DataGridHeader_Checked(object sender, RoutedEventArgs e)
{
DataGrid dg = SGT.Helpers.ControlHelpers.Generics.FindVisualParent<DataGrid>(sender as DependencyObject, typeof(DataGrid));
for (int i = 0; i < dg.Items.Count; i++)
{
DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
CheckBox c = ControlHelpers.CheckBoxes.ToggleCheckBox(row, true);
}
e.Handled = true;
}
未檢查
private void DataGridHeader_Unchecked(object sender, RoutedEventArgs e)
{
DataGrid dg = SGT.Helpers.ControlHelpers.Generics.FindVisualParent<DataGrid>(sender as DependencyObject, typeof(DataGrid));
for (int i = 0; i < dg.Items.Count; i++)
{
DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
CheckBox c = ControlHelpers.CheckBoxes.ToggleCheckBox(row, false);
}
e.Handled = true;
}
不定
private void DataGridHeader_Indeterminate(object sender, RoutedEventArgs e)
{
((CheckBox)sender).IsChecked = false;
e.Handled = true;
}
切換
public static CheckBox ToggleCheckBox(DataGridRow row, bool? overrideValue)
{
if (row == null)
{
return null;
}
CheckBox c = Generics.FindVisualChild<CheckBox>(row as DependencyObject, typeof(CheckBox));
if (overrideValue != null)
{
c.IsChecked = overrideValue;
}
else
{
c.IsChecked = !c.IsChecked;
}
return c;
}
public static CheckBox ToggleCheckBox(object sender, MouseButtonEventArgs e, bool? overrideValue)
{
DataGridRow row = ItemsControl.ContainerFromElement((DataGrid)sender, e.OriginalSource as DependencyObject) as DataGridRow;
if (row == null)
{
return null;
}
CheckBox c = Generics.FindVisualChild<CheckBox>(row as DependencyObject, typeof(CheckBox));
if (overrideValue != null)
{
c.IsChecked = overrideValue;
}
else
{
c.IsChecked = !c.IsChecked;
}
e.Handled = true;
return c;
}
查找視覺
public static T FindVisualChild<T>(DependencyObject parent, Type type) where T : DependencyObject
{
if (parent == null)
Debug.Print("Parent Null");
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child.GetType() == type)
{
Debug.Print(child.ToString());
return child as T;
}
else
{
T result = FindVisualChild<T>(child, type);
if (result != null)
return result;
}
}
return null;
}
DataGrid中點擊了
private void DataGrid_MouseUp(object sender, MouseButtonEventArgs e)
{
CheckBox c = ControlHelpers.CheckBoxes.ToggleCheckBox(sender, e, null);
ValidateCheckAll((DataGrid)sender);
}
柄頭複選框,當項目選中/取消
private void ValidateCheckAll(DataGrid grid)
{
//HeaderedContentControl
DataGridColumnHeadersPresenter hp = Helpers.ControlHelpers.Generics.FindVisualChild<DataGridColumnHeadersPresenter>(grid, typeof(DataGridColumnHeadersPresenter));
CheckBox c = Helpers.ControlHelpers.Generics.FindVisualChild<CheckBox>(hp, typeof(CheckBox));
c.Checked -= new RoutedEventHandler(DataGridHeader_Checked);
c.Unchecked -= new RoutedEventHandler(DataGridHeader_Unchecked);
c.Indeterminate -= new RoutedEventHandler(DataGridHeader_Indeterminate);
int counter = 0;
for (int i = 0; i < grid.Items.Count; i++)
{
DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(i);
//row.Item
if (row != null)
{
CheckBox ch = Helpers.ControlHelpers.Generics.FindVisualChild<CheckBox>(row, typeof(CheckBox));
if (ch.IsChecked == true)
{
counter++;
}
}
Debug.Print(counter.ToString() + " of " + grid.Items.Count.ToString());
}
if (counter == 0)
{
c.IsChecked = false;
}
else if (counter == grid.Items.Count)
{
c.IsChecked = true;
}
else
{
c.IsChecked = null;
}
c.Checked += new RoutedEventHandler(DataGridHeader_Checked);
c.Unchecked += new RoutedEventHandler(DataGridHeader_Unchecked);
c.Indeterminate += new RoutedEventHandler(DataGridHeader_Indeterminate);
}
我有一切工作我預想的樣子,直到..我得到了一個有可滾動數據的DataGrid。
問題:只有可見的複選框發生變化。我使用視覺效果來獲得複選框,並略微理解爲什麼會發生這種情況。
問題:有沒有辦法將CheckBox的其餘部分改爲視圖外?
DataGridHeader_Checked和DataGridHeader_Unchecked的實現在哪裏? – Xiaoy312
添加相關代碼 – Jmyster