以编程方式取消选择DataGrid中的行

时间:2012-11-29 14:36:49

标签: c# wpf datagrid selection wpfdatagrid

我很难找到解决方案。

我要做的是只在DataGrid中选择某些行。 SelectionMode是FullRow。例如,如果用户试图拖动选择几行,而其中一行我不希望是可选择的。在这种情况下,我希望仍然选择有效行,但不能选择无效行。

有什么想法吗?

4 个答案:

答案 0 :(得分:1)

这不是最好的方法,但你可以创建一个包含所选网格索引的inherted类,然后如果选择了无效行,你只需将所选索引更改为最后一个有效索引

答案 1 :(得分:1)

This guy wanted to do something alike with a ListBox。我相信该解决方案也可以适用于DataGrid。

修改

public static class DataGridRowEx
{
    public static bool GetCanSelect(DependencyObject obj)
    {
        return (bool)obj.GetValue(CanSelectProperty);
    }
    public static void SetCanSelect(DependencyObject obj, bool value)
    {
        obj.SetValue(CanSelectProperty, value);
    }
    public static readonly DependencyProperty CanSelectProperty =
        DependencyProperty.RegisterAttached("CanSelect", typeof(bool), typeof(DataGridRowEx), new UIPropertyMetadata(true, OnCanSelectChanged));

    private static void OnCanSelectChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
    {
        var item = sender as DataGridRow;
        if (item == null)
            return;

        if ((bool)args.NewValue)
        {
            item.Selected -= RowSelected;
        }
        else
        {
            item.Selected += RowSelected;
            item.IsSelected = false;
        }
    }

    private static void RowSelected(object sender, RoutedEventArgs e)
    {
        var item = sender as DataGridRow;
        if (item == null)
            return;

        item.Dispatcher.BeginInvoke((Action)(()=>
        item.IsSelected = false));
    }
}

测试它:

public class ViewModel : INotifyPropertyChanged
{
    #region INotifyPropertyChanged values

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion


    public List<Dummy> Elements { get; set; }

    public ViewModel()
    {
        this.Elements = new List<Dummy>(){
            new Dummy() { CanSelect =true, MyProperty = "Element1"},
            new Dummy() { CanSelect =false, MyProperty = "Element2"},
            new Dummy() { CanSelect =true, MyProperty = "Element3"},
            new Dummy() { CanSelect =false, MyProperty = "Element4"},
            new Dummy() { CanSelect =true, MyProperty = "Element5"},
            new Dummy() { CanSelect =true, MyProperty = "Element6"},
            new Dummy() { CanSelect =true, MyProperty = "Element7"},
            new Dummy() { CanSelect =true, MyProperty = "Element8"},
            new Dummy() { CanSelect =false, MyProperty = "Element9"},
        };
    }
}

public class Dummy
{
    public bool CanSelect { get; set; }

    public string MyProperty { get; set; }

    public override string ToString()
    {
        return this.MyProperty;
    }
}

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="525"
    Height="350"
    mc:Ignorable="d">
<Window.Resources>
    <Style x:Key="DataGridRowStyle" TargetType="{x:Type DataGridRow}">
        <Setter Property="local:DataGridRowEx.CanSelect" Value="{Binding CanSelect}" />
    </Style>
</Window.Resources>
<Window.DataContext>
    <local:ViewModel />
</Window.DataContext>
<Grid x:Name="LayoutRoot">
    <DataGrid ItemsSource="{Binding Elements}"
              RowStyle="{DynamicResource DataGridRowStyle}"
              SelectionUnit="FullRow" />
</Grid>
</Window>

即使按下shift,它也可以进行多选。与ListBoxItem解决方案唯一的显着区别是unselection必须使用Dispatcher.BeginInvoke排队,不知道为什么。 这种方法唯一需要注意的是,如果DataGrid具有单个选择,则尝试选择一个不可选择的项目将取消选择当前选定的项目,如果DataGrid具有扩展选择,则取消选择all。

答案 2 :(得分:0)

不是一个很好的解决方案但你可以在Mouse_Up事件中取消选择行,所以让用户选择all然后以编程方式取消选择,我还没有测试过这个

private void dgvReport_MouseUp(object sender, MouseEventArgs e)
        {

            foreach (DataGridViewRow row in this.dgvReport.SelectedRows) {


                if (row.Cells[1].Value == "Invalid"){

                    this.dgvReport.Rows[row.Index].Selected = false;

                }


            }
        }

答案 3 :(得分:0)

这将有效,

        int row = grdexam.SelectedIndex;
        DataGridRow rv =(DataGridRow)this.grdexam.ItemContainerGenerator.ContainerFromIndex(row);
        DataRowView rvv =(DataRowView)rv.Item;
        MessageBox.Show(rvv.Row[1].ToString());