8

嘿,

我需要帮助弄清楚如何访问 wpf datagrid 标题中的复选框。这是我所拥有的:

<DataGrid.Columns>
    <DataGridTemplateColumn CanUserReorder="False" CanUserResize="False">
        <DataGridTemplateColumn.HeaderTemplate>
            <DataTemplate>
                <CheckBox Name="cbxAll" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" />
            </DataTemplate>
        </DataGridTemplateColumn.HeaderTemplate>
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <CheckBox IsChecked="{Binding Path=Checked, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding Path=NoErrors}" Name="theCheckbox" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

我已经设置了Name复选框,但由于某种原因我无法从代码端访问它。

刷新我的数据网格项目后,我需要访问复选框以取消选中它。我怎样才能做到这一点?

4

3 回答 3

9

MVVM 中的一个工作示例:

视图模型

public class MainWindowViewModel : INotifyPropertyChanged
{
    private bool allItemsAreChecked;

    public event PropertyChangedEventHandler PropertyChanged;

    public bool AllItemsAreChecked
    {
        get
        {
            return this.allItemsAreChecked;
        }
        set
        {
            this.allItemsAreChecked = value;
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs("AllItemsAreChecked"));
            }
        }
    }
}

XAML

<DataGridTemplateColumn CanUserReorder="False" CanUserResize="False">
    <DataGridTemplateColumn.HeaderTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding
                RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
                Path=DataContext.AllItemsAreChecked}" />
        </DataTemplate>
    </DataGridTemplateColumn.HeaderTemplate>
</DataGridTemplateColumn>
于 2013-02-07T11:55:19.210 回答
3

尽管绑定可能是您应该采用的方式,但您可以按照您的要求进行操作。这是一种方法:

1.给你的标题CheckBox一个 Uid

<CheckBox Uid="CheckAll" />

2.命名你的DataGrid

<DataGrid Name="myDataGrid" />

3.实现如下扩展方法

public static UIElement FindUid(this DependencyObject parent, string uid)
{
    var count = VisualTreeHelper.GetChildrenCount(parent);
    if (count == 0) return null;

    for (int i = 0; i < count; i++)
    {
        var el = VisualTreeHelper.GetChild(parent, i) as UIElement;
        if (el == null) continue;

        if (el.Uid == uid) return el;

        el = el.FindUid(uid);
        if (el != null) return el;
    }
    return null;
}

4.像这样访问并取消选中CheckBox后面的代码

CheckBox checkBox = myDataGrid.FindUid("CheckAll") as CheckBox;
checkBox.IsChecked = false;
于 2013-02-07T08:04:29.007 回答
0

设置一个简单的事件:

<CheckBox x:Name="cbxAll" Click="cbxAll_Click"/>

在事件中点击代码:

private void cbxAll_Click(object sender, RoutedEventArgs e)
{
   var ckbox = sender as CheckBox;
   if (ckbox.IsChecked == true)
   {
    //Check all itens
   }
   else
   {
    //Uncheck all itens
   }
}

注意:发件人是复选框

于 2017-07-23T17:30:46.437 回答