2

我正在努力寻找解决方案。

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

有任何想法吗?

4

4 回答 4

1

这家伙想用 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 解决方案的唯一显着区别是取消选择必须使用 Dispatcher.BeginInvoke 进行排队,不知道为什么。这种方法的唯一警告是,如果 DataGrid 具有单个选择,则尝试选择不可选择的项目会取消选择当前选定的项目,或者如果 DataGrid 具有扩展选择,则取消选择所有项目。

于 2012-11-29T14:47:17.430 回答
1

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

于 2012-11-29T14:45:59.673 回答
0

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

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;

                }


            }
        }
于 2012-11-29T14:53:58.830 回答
0

这将起作用,

        int row = grdexam.SelectedIndex;
        DataGridRow rv =(DataGridRow)this.grdexam.ItemContainerGenerator.ContainerFromIndex(row);
        DataRowView rvv =(DataRowView)rv.Item;
        MessageBox.Show(rvv.Row[1].ToString());
于 2013-04-09T12:08:58.847 回答