如何防止在第一次单击时选择整个DataGrid行?

时间:2011-10-18 21:32:00

标签: wpf mvvm datagrid checkbox toolkit

我有WPF Took Kit DataGrid with checkbox。用户应该能够单击一下检查或取消选中项目。但是我遇到了以下鼠标点击行为问题。

在第一次鼠标单击时,它选择整行,并在随后的单击中选中或取消选中复选框。

现在我该如何避免第一次鼠标点击行为,以便用户不必多次点击?

我更喜欢带有复选框的DataGrid而没有像WPF Tool Kit这样的第三方工具。如果没有WPF工具包,有人可以提供示例代码的URL,我将不胜感激。

TasklistView.xaml

 <UserControl x:Class="MyProject.TasklistView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="742" Width="1024" >

  <Grid>
    <toolkit:DataGrid x:Name="dgMyTaskList" 
                      SelectionMode="Single"  
                      ItemsSource="{Binding Path=MyTaskList}"
                      AutoGenerateColumns="False"                                      
                      SelectedItem="{Binding Path=SelectedTaskItem,
                      UpdateSourceTrigger=PropertyChanged}" >



        <toolkit:DataGrid.Columns>
            <toolkit:DataGridCheckBoxColumn 
                      Header="Select" 
                      Binding="{Binding Path=IsSelected}"  />

            <toolkit:DataGridTextColumn 
                      Header="Task ID" 
                      Width="100" 
                      Binding="{Binding TaskID}" 
                      IsReadOnly="True" />

            <toolkit:DataGridTextColumn 
                      Header="Task Description" 
                      Width="*" Binding="{Binding TaskDescription}"
                      IsReadOnly="True" />

            <toolkit:DataGridTextColumn 
                      Header="Due Date" Width="*" 
                      Binding="{Binding DueDate}" 
                      IsReadOnly="True" />

        </toolkit:DataGrid.Columns>
    </toolkit:DataGrid>
   </Grid>
 </UserControl>

TasklistViewModel.cs

 using System;
 using System.ComponentModel;
 using System.Collections.Generic;
 using System.Windows.Forms;
 using System.Windows;
 using System.Windows.Data;
 using System.Windows.Input;
 using System.Windows.Controls;
 using System.Collections.ObjectModel;
 using System.Text;
 using System.IO;


 namespace MyProject
 {
  public class TasklistViewModel: ViewModelBase
  {

    ObservableCollection<TaskModel> _TaskList;
    public TasklistViewModel()
    {
        _MyTaskList= dal.GetTaskList());
    }

    public ObservableCollection<TaskModel> MyTaskList
    {
        get { return _MyTaskList; }
        set
        {
            if (_MyTaskList!= value)
            {
                this._MyTaskList= value;
                this.OnPropertyChanged("MyTaskList");
            }
        }

    }

    private TaskModel _SelectedTaskItem;
    public TaskModel SelectedTaskItem
    {
        get { return _SelectedTaskItem; }
        set
        {

            if (value != null)
            {
                _SelectedTaskItem= value;
                OnPropertyChanged("SelectedTaskItem");

                if (null != _SelectedTaskItem)
                {
                    ObservableCollection<TaskModel> oCol =
                        new ObservableCollection<TaskModel>();
                    foreach (TaskModel itm in MyTaskList)
                    {
                        if (itm.TaskID == _SelectedTaskItem.BundleID)
                        {
                            itm.IsSelected = _SelectedTaskItem.IsSelected;
                        }
                        oCol.Add(itm);

                    }
                    MyTaskList.Clear();
                    MyTaskList = oCol;
                    OnPropertyChanged("MyTaskList");
                }
            }
        }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

我通过在包含复选框

的列上应用单元格样式解决了同样的问题

MyWindow.xaml

    <DataGridCheckBoxColumn Header="Select" Binding="{Binding Path=IsSelected}">
      <DataGridCheckBoxColumn.CellStyle>
        <Style TargetType="DataGridCell">
          <EventSetter Event="MouseLeftButtonUp" Handler="ApprovedMouseUp"></EventSetter>
        </Style>
      </DataGridCheckBoxColumn.CellStyle>
    </DataGridCheckBoxColumn>

MyWindow.cs

    private void ApprovedMouseUp(object sender, MouseButtonEventArgs e)
    {
        if(sender is DataGridCell)
        {
            var temp = (sender as DataGridCell).Content;
            if(temp is CheckBox) (temp as CheckBox).IsChecked = !(temp as CheckBox).IsChecked;
        }
    }

我没有使用WPF工具包,而是使用DataGrid i .NET 4.0。 从来没有,我相信它无论如何都会起作用