8

我需要一些 c# 代码来选择/取消选择 WPF 3.5 框架中数据网格中的所有复选框。我想通过单击网格中的单个标题复选框来执行此操作。

请帮忙。

4

5 回答 5

6

这可以以声明方式完成。下面为每一行创建一个复选框列,并且可以切换行选择。可以单击复选框列的标题以选择所有行。

xaml 中的相关部分

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit">
    <toolkit:DataGrid Name="dataGrid" 
     ItemsSource="{Binding}" AutoGenerateColumns="True" 
     SelectionMode="Extended" CanResizeRows="False">
    <toolkit:DataGrid.RowHeaderTemplate>
       <DataTemplate>
           <Grid>
               <CheckBox IsChecked="{
                  Binding Path=IsSelected, 
                  Mode=TwoWay, 
                  RelativeSource={RelativeSource FindAncestor, 
                  AncestorType={x:Type toolkit:DataGridRow}}}"
            />
           </Grid>
       </DataTemplate>
    </toolkit:DataGrid.RowHeaderTemplate>
    </toolkit:DataGrid>
</Window>
于 2009-07-22T02:43:07.593 回答
5

这是我们在 .Net 4.0 XAML 文件中使用的示例数据网格:

<DataGrid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Name="dgMissingNames" ItemsSource="{Binding Path=TheMissingChildren}" Style="{StaticResource NameListGrid}" SelectionChanged="DataGrid_SelectionChanged">
    <DataGrid.Columns>
        <DataGridTemplateColumn CellStyle="{StaticResource NameListCol}">
            <DataGridTemplateColumn.HeaderTemplate>
                <DataTemplate>
                    <CheckBox Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" />
                </DataTemplate>                            
            </DataGridTemplateColumn.HeaderTemplate>
            <DataGridTemplateColumn.CellTemplate>                        
                <DataTemplate>
                    <CheckBox IsChecked="{Binding Path=Checked, UpdateSourceTrigger=PropertyChanged}" Name="theCheckbox"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>                            
        </DataGridTemplateColumn>
        <DataGridTextColumn Binding="{Binding Path=SKU}" Header="Album" CellStyle="{StaticResource NameListCol}"/>
        <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" CellStyle="{StaticResource NameListCol}"/>
        <DataGridTextColumn Binding="{Binding Path=Pronunciation}" Header="Pronunciation" CellStyle="{StaticResource NameListCol}"/>
    </DataGrid.Columns>
</DataGrid>

这是代码隐藏:

private void HeadCheck(object sender, RoutedEventArgs e, bool IsChecked)
{
    foreach (CheckedMusicFile mf in TheMissingChildren)
    {
        mf.Checked = IsChecked;
    }
    dgMissingNames.Items.Refresh();
}

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
    HeadCheck(sender, e, true);
}

private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
    HeadCheck(sender, e, false);
}

TheMissingChildren 是一个简单的对象结构,具有一些字符串属性和一个 ischecked 布尔值。

HTH。

于 2011-05-05T05:09:03.233 回答
2

This is based on someone else's source that I can't recall, but we use it to help find visual children of a type. It may not be the most efficient use for this scenario but it might help get you on the right track.

    public static childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(obj, i);
            if (child != null && child is childItem)
                return (childItem)child;

            childItem childOfChild = FindVisualChild<childItem>(child);
            if (childOfChild != null)
                return childOfChild;
        }
        return null;
    }

[Edit 4.16.09] Based on that, try out this method. Should find all CheckBoxes and change the state as provided, callable from your event handler on the Checked/Unchecked events.

   public static void CheckAllBoxes(DependencyObject obj, bool isChecked)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            // If a checkbox, change IsChecked and continue.
            if (obj is CheckBox)
            {
                ((CheckBox) obj).IsChecked = isChecked;
                continue;
            }

            DependencyObject child = VisualTreeHelper.GetChild(obj, i);
            CheckAllBoxes(child, isChecked);
        }
    }
于 2009-04-16T14:07:27.057 回答
0

我会在 WPF 中使用新的数据绑定功能。为所有复选框的 IsChecked 属性提供单向绑定,绑定源是主复选框的 IsChecked 属性。

另一种选择是使用触发器

于 2009-04-20T20:22:13.307 回答
0
DataColumn dt = null;            
        for (int i = 0; i < dataGrid.Columns.Count; i++)
        {

     dt = new DataColumn("Column Name");

            dt.DataType = typeof(Boolean);
            dt.DefaultValue = false;
             dataTable.Add(dt);
        }

dataGied.DataContext = dataTable;



<Custom:DataGrid x:Name="dataGrid" AutoGenerateColumns="True" ItemsSource="{Binding}" />                    

好吧,这只是一个粗略的想法,希望这可行

于 2009-11-04T13:50:00.507 回答