嗨我有wpf应用程序,其中我有一个数据网格,标题中有3个复选框。
当选中任何标题复选框时,我希望检查该特定部分中的所有正文复选框。
问题是当我检查标题时,只检查视图中可见的复选框。当我使用滚动条向下滚动时,向下移动scollbar时可见的不可见复选框,将取消选中这些复选框。有谁可以帮忙。谢谢。这是代码。
public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
// If there is a child found and if the child is of the T type.
//Dont remove null check . If no check i
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
private void ChkHeaderExtract_OnChecked(object sender, RoutedEventArgs e)
{
var extractCheckBoxes = GetAllCheckBoxs();
var i = 0;
foreach (var chk in extractCheckBoxes)
{
if (i % 3 == 0)
{
chk.IsChecked = true;
}
i++;
}
}
XAML:
<DataGrid x:Name="DgEntities" FrozenColumnCount="13" IsReadOnly="True" MaxWidth="854" Height="444" CanUserAddRows="false" ItemsSource="{Binding JobEntitiesCollectionViewSource.View, Mode=OneWay, NotifyOnTargetUpdated=True}" AutoGenerateColumns="False" SelectionMode="Single" SelectionUnit="FullRow" HorizontalScrollBarVisibility="Disabled" Margin="21,-49,106,5" Width="763">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="Entity" Width="220" MinWidth="172" MaxWidth="215"/>
<DataGridTemplateColumn Header="Status" Width="138" MaxWidth="138" MinWidth="138">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Margin="5" Content="{Binding ExtractStatus, Mode=TwoWay, NotifyOnTargetUpdated=True}" Command="{Binding DataContext.HyperlinkCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" CommandParameter="{Binding SelectedItem, ElementName=DgEntities}" Foreground="Blue" Cursor="Hand" MouseDoubleClick="Control_OnMouseDoubleClick" >
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<TextBlock TextDecorations="Underline"><InlineUIContainer>
<ContentPresenter />
</InlineUIContainer></TextBlock>
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="40" MaxWidth="40" MinWidth="40">
<DataGridTemplateColumn.Header>
<CheckBox x:Name="ChkHeaderExtract" Checked="ChkHeaderExtract_OnChecked" Unchecked="ChkHeaderExtract_OnUnchecked"/>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox HorizontalAlignment="Center" Unchecked="ChkExtract_OnUnchecked" VerticalAlignment="Center" x:Name="chkExtract" IsChecked="{Binding ExtractIsSelected, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Status" Width="128" MaxWidth="128" MinWidth="128">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Margin="5" Content="{Binding TransformStatus, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" Command="{Binding DataContext.HyperlinkCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" CommandParameter="{Binding SelectedItem, ElementName=DgEntities}" Cursor="Hand" Foreground="Blue" MouseDoubleClick="Control_OnMouseDoubleClick_2" >
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<TextBlock TextDecorations="Underline"><InlineUIContainer>
<ContentPresenter />
</InlineUIContainer></TextBlock>
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="40" MaxWidth="40" MinWidth="40">
<DataGridTemplateColumn.Header>
<CheckBox x:Name="ChkHeaderTransform" Checked="ChkHeaderTransform_OnChecked" Unchecked="ChkHeaderTransform_OnUnchecked"/>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox HorizontalAlignment="Center" Unchecked="ChkTransform_OnUnchecked" VerticalAlignment="Center" x:Name="chkTransform" IsChecked="{Binding TransformIsSelected, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Status" Width="137.5" MaxWidth="137.5" MinWidth="137.5">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Margin="5" Content="{Binding LoadStatus, Mode=TwoWay, NotifyOnTargetUpdated=True}" Command="{Binding DataContext.HyperlinkCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" CommandParameter="{Binding SelectedItem, ElementName=DgEntities}" Cursor="Hand" Foreground="Blue" MouseDoubleClick="Control_OnMouseDoubleClick" >
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<TextBlock TextDecorations="Underline"><InlineUIContainer>
<ContentPresenter />
</InlineUIContainer></TextBlock>
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="56" MaxWidth="56" MinWidth="56">
<DataGridTemplateColumn.Header>
<CheckBox x:Name="ChkHeaderLoad" HorizontalAlignment="Center" Margin="0.3" Checked="ChkHeaderLoad_OnChecked" Unchecked="ChkHeaderLoad_OnUnchecked" />
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Margin="8" x:Name="chkLoad" Checked="ChkLoad_OnChecked" Unchecked="ChkLoad_OnUnchecked" IsChecked="{Binding LoadIsSelected, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
答案 0 :(得分:-1)
这只会返回已加载的孩子..
VisualTreeHelper.GetChildrenCount(depObj)
你应该更新实际的绑定对象,然后真正刷新itemssource。