DataGridCheckBoxColumn 的单击事件 [英] Click event for DataGridCheckBoxColumn

查看:25
本文介绍了DataGridCheckBoxColumn 的单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 DataGridCheckBoxColumn 的 WPF 表单中的 DataGrid,但我没有找到任何点击事件,已选中和未选中它...

这些事件是否可用于 DataGridCheckBoxColumn?如果没有,请提出一些我可以使用的解决方法.

解决方案

引自 William Han 的回答:http://social.msdn.microsoft.com/Forums/ar/wpf/thread/9e3cb8bc-a860-44e7-b4da-5c8b8d40126d>

它只是向列添加一个事件.这是一个很好的简单解决方案.

<块引用>

也许你可以使用 EventSetter 作为下面的例子:

标记:

<窗口.资源><local:People x:Key="People"/></Window.Resources><网格><DataGrid ItemsSource="{StaticResource People}" AutoGenerateColumns="False"><DataGrid.Columns><DataGridTextColumn Binding="{Binding Path=Name}" Header="Name"/><DataGridCheckBoxColumn Binding="{Binding Path=LikeCar}" Header="LikeCar"><DataGridCheckBoxColumn.CellStyle><风格><EventSetter Event="CheckBox.Checked" Handler="OnChecked"/></风格></DataGridCheckBoxColumn.CellStyle></DataGridCheckBoxColumn></DataGrid.Columns></DataGrid></网格></窗口>

代码:

使用系统;使用 System.Windows;命名空间 DataGridCheckBoxColumnTest{///<总结>///MainWindow.xaml 的交互逻辑///</总结>公共部分类 MainWindow : 窗口{公共主窗口(){初始化组件();}void OnChecked(对象发送者,RoutedEventArgs e){抛出新的 NotImplementedException();}}}命名空间 DataGridCheckBoxColumnTest{公开课人{公共人(字符串名称,bool likeCar){姓名 = 姓名;LikeCar = likeCar;}公共字符串名称{设置;得到;}public bool LikeCar { set;得到;}}}使用 System.Collections.Generic;命名空间 DataGridCheckBoxColumnTest{公共类人物:列表<​​人物>{公众人物(){添加(新人(汤姆",假));添加(新人(仁",假));}}}

I have a DataGrid in a WPF form with a DataGridCheckBoxColumn, but I did not find any click event, Checked and unchecked for it...

Are these events available for the DataGridCheckBoxColumn? If not please suggest some workaround I could use.

解决方案

Quoted from William Han's answer here: http://social.msdn.microsoft.com/Forums/ar/wpf/thread/9e3cb8bc-a860-44e7-b4da-5c8b8d40126d

It simply adds an event to the column. It is a good simple solution.

Perhaps you can use EventSetter as example below:

Markup:

<Window x:Class="DataGridCheckBoxColumnTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DataGridCheckBoxColumnTest"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:People x:Key="People"/>
    </Window.Resources>
    <Grid>
        <DataGrid ItemsSource="{StaticResource People}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name"/>
                <DataGridCheckBoxColumn Binding="{Binding Path=LikeCar}" Header="LikeCar">
                    <DataGridCheckBoxColumn.CellStyle>
                        <Style>
                            <EventSetter Event="CheckBox.Checked" Handler="OnChecked"/>
                        </Style>
                    </DataGridCheckBoxColumn.CellStyle>
                </DataGridCheckBoxColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

Code:

using System;
using System.Windows;

namespace DataGridCheckBoxColumnTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        void OnChecked(object sender, RoutedEventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}


namespace DataGridCheckBoxColumnTest
{
    public class Person
    {
        public Person(string name, bool likeCar)
        {
            Name = name;
            LikeCar = likeCar;
        }
        public string Name { set; get; }
        public bool LikeCar { set; get; }
    }
}

using System.Collections.Generic;

namespace DataGridCheckBoxColumnTest
{
    public class People : List<Person>
    {
        public People()
        {
            Add(new Person("Tom", false));
            Add(new Person("Jen", false));
        }
    }
}

这篇关于DataGridCheckBoxColumn 的单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆