簡體   English   中英

以編程方式取消選擇 DataGrid 中的一行

[英]Programmatically deselect a row in a DataGrid

我正在努力尋找解決方案。

我想要做的是只在 DataGrid 中選擇某些行。 SelectionMode 是 FullRow。 一個例子是,如果用戶試圖拖動選擇幾行,我不想選擇其中的一行。 在這種情況下,我希望仍然選擇有效行,而不是無效行。

有任何想法嗎?

這不是最好的方法,但您可以創建一個繼承類來保存網格的選定索引,然后如果選擇了無效行,您只需將選定索引更改為最后一個有效索引

這家伙想用 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 具有擴展選擇,則取消選擇所有項目。

不是一個很好的解決方案,但您可以取消選擇 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;

                }


            }
        }

這將工作,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM