单击DataGridCheckBoxColumn的事件

问题描述 投票:17回答:5

我有一个带有DataGrid的WPF格式的DataGridCheckBoxColumn,但我没有找到任何点击事件,检查并取消选中它...

这些活动是否适用于DataGridCheckBoxColumn?如果没有,请建议我可以使用的一些解决方法。

wpf events datagrid wpfdatagrid
5个回答
22
投票

来自William Han的回答引用:qazxsw poi

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

也许你可以使用http://social.msdn.microsoft.com/Forums/ar/wpf/thread/9e3cb8bc-a860-44e7-b4da-5c8b8d40126d作为例子如下:

标记:

EventSetter

码:

<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>

4
投票

扩展了上面提到的DataGridCell概念,这就是我们过去使用它的方法。

... ... XAML

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));
        }
    }
}

TheMissingChildren是一个ObservableCollection对象,它包含数据元素列表,包括我们用来填充数据网格的布尔字段“Checked”。

这里的SelectionChanged代码将在底层的TheMissingChildren对象中设置checked布尔值,并触发项目列表的刷新。这样可以确保无论您在何处单击该行,都会检查该框并显示新状态。单击复选框或行中的某个位置将打开/关闭检查。

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

1
投票

这样的事情怎么样?

private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DataGrid ThisGrid = (DataGrid)sender;
    CheckedMusicFile ThisMusicfile = (CheckedMusicFile)ThisGrid.SelectedItem;
    ThisMusicfile.Checked = !ThisMusicfile.Checked;
    ThisGrid.Items.Refresh();
}

然后在XAML中

partial class SomeAwesomeCollectionItems : INotifyPropertyChanged
{
    public event PropertyChanged;
    protected void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(property);
    }

    private bool _IsSelected;
    public bool IsSelected { get { return _IsSelected; } set { _IsSelected = Value; OnPropertyChanged("IsSelected"); } }
}

但是,使用这种方法的一个注意事项是,您可能会遇到虚拟化和检查项目未清除的问题(不确定,未使用SelectionMode =“Single”进行测试)。如果是这种情况,我发现最简单的解决方法是关闭虚拟化,但也许有更好的方法来解决这个特定问题。


0
投票
<DataGrid ItemsSource="{Binding Path=SomeAwesomeCollection"} SelectionMode="Single">
    <DataGrid.Resources>
        <Style TargetType="{x:Type DataGridRow}"
               BasedOn="{StaticResource {x:Type DataGridRow}}">
        <!--Note that you will probably need to base on other style if you have stylized your DataGridRow-->
            <Setter Property="IsSelected" Value="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" />
        </Style>
    </DataGrid.Resources
    <DataGrid.Columns>
        <DataGridCheckBoxColumn Binding="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" />
        <!--More Columns-->
    </DataGrid.Columns>
</DataGrid>

0
投票

如果您不想将事件添加到您的样式中,您也可以这样做。

<wpf:DataGridCheckBoxColumn Header="Cool?" Width="40" Binding="{Binding IsCool}"/>
© www.soinside.com 2019 - 2024. All rights reserved.