在 XamDataGrid C# 中查找 select 行

Find and select row in XamDataGrid C#

如何按列搜索行,然后 select 在 XamDataGrid 中搜索该行。 我试过遍历 DataSource,但是元素的类型不是很有用,它只暴露了一个 HasData bool 属性。

尝试使用 XamDataGrid.FieldLayouts.DataPresenter.Records 集合并检查所需的单元格。当找到记录时,可以通过设置 record.IsSelected = true; 来选择它 类似的东西:

using System;
using System.Windows;
using System.Windows.Media;

using System.Collections.ObjectModel;
using System.ComponentModel;
using Infragistics.Windows.DataPresenter;

namespace IGFindRow
{  
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            _cars = Cars;
            InitializeComponent();
        }        

        #region Code fragment from samples provided by Infragistics 

        public ObservableCollection<Car> _cars = null;

        public ObservableCollection<Car> Cars
        {
            get
            {
                if (this._cars == null)
                {
                    this._cars = new ObservableCollection<Car>();
                    this._cars.Add(new Car("Dodge", "Ram", Colors.Blue, 22050.00, 153));
                    this._cars.Add(new Car("Ford", "Explorer", Colors.Green, 27175.00, 96));
                    this._cars.Add(new Car("BMW", "Z4", Colors.Silver, 35600.00, 42));
                    this._cars.Add(new Car("Toyota", "Camry", Colors.Black, 20790.99, 131));
                }

                return _cars;
            }
        }

        public class Car : INotifyPropertyChanged
        {
            string m_make;
            string m_model;
            Color m_color;
            double m_baseprice;
            int m_milage;

            public Car(string make, string model, Color color, double baseprice, int milage)
            {
                this.Make = make;
                this.Model = model;
                this.Color = color;
                this.BasePrice = baseprice;
                this.Milage = milage;
            }

            public string Make
            {
                get { return m_make; }
                set
                {
                    if (m_make != value)
                    {
                        m_make = value;
                        NotifyPropertyChanged("Make");
                    }
                }
            }

            public string Model
            {
                get { return m_model; }
                set
                {
                    if (m_model != value)
                    {
                        m_model = value;
                        NotifyPropertyChanged("Model");
                    }
                }
            }

            public Color Color
            {
                get { return m_color; }
                set
                {
                    if (m_color != value)
                    {
                        m_color = value;
                        NotifyPropertyChanged("Color");
                    }
                }
            }

            public double BasePrice
            {
                get { return m_baseprice; }
                set
                {
                    if (m_baseprice != value)
                    {
                        m_baseprice = value;
                        NotifyPropertyChanged("BasePrice");
                    }
                }
            }

            public int Milage
            {
                get { return m_milage; }
                set
                {
                    if (m_milage != value)
                    {
                        m_milage = value;
                        NotifyPropertyChanged("Milage");
                    }
                }
            }


            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged(String info)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
            }

        }

        #endregion

        private void Search_Click(object sender, RoutedEventArgs e)
        {
            // Enumerate records 
            foreach (var it in dataGrid.FieldLayouts.DataPresenter.Records)
            {
                if (it is DataRecord record)
                {
                    // Check the current column value
                    if (record.Cells["Make"].Value.ToString().ToUpper() == Maker.Text.ToUpper())
                    {
                        record.IsSelected = true;
                        break;
                    }
                }
            }
        }
    } 
}

XAML:

<Window x:Class="IGFindRow.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:ig="http://infragistics.com/DataPresenter"        
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" 
        Name="dgTest">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="5,5,5,5">
            <Label Name="ColumnName" Padding="5,5,0,5">Maker:</Label>
            <TextBox Name="Maker" Padding="5,5,5,5" Margin="3,0,20,0">Ford</TextBox>
            <Button Name="Search" Padding="5,5,5,5" Click="Search_Click">Press to search</Button>
        </StackPanel>
        <ig:XamDataGrid x:Name="dataGrid" Grid.Row="1" 
                          IsGroupByAreaExpanded="False" 
                          GroupByAreaLocation="None" 
                          Theme="Generic"
                          DataSource="{Binding ElementName=dgTest, Path=Cars}">

            <ig:XamDataGrid.FieldLayoutSettings>
                <ig:FieldLayoutSettings SelectionTypeRecord="Single" />
            </ig:XamDataGrid.FieldLayoutSettings>

            <ig:XamDataGrid.ViewSettings>
                <ig:GridViewSettings/>
            </ig:XamDataGrid.ViewSettings>          

            <ig:XamDataGrid.FieldSettings>
                <ig:FieldSettings CellClickAction="SelectRecord" AllowRecordFiltering="True"/>
            </ig:XamDataGrid.FieldSettings>

        </ig:XamDataGrid>
    </Grid>
</Window>