使用 XamDataGrid 并尝试从 RecordActivated() 函数的发送者对象中获取特殊值

Working with XamDataGrid and trying to get a special value out of a sender object from the RecordActivated() function

你好,

我遇到了一个问题,我想不出解决办法。
由于我在基础设施和 wpf 方面没有太多经验,所以我首先创建了一个测试项目来尝试如何正确地完成它。

我得到了一个包含组合框的 xamComboEditor。
当我打开组合框时,我得到了一个带有 "states"-table 的 xamDataGrid。 选择一行后,我需要将所选行的主键写入comboBox。
我的问题是,当 RecordActivated()-Event 触发时,我得到了发件人对象,但我不知道如何从中读取我需要的数据。
当我使用 VisualStudio 监视列表工具查看发件人对象时,我可以找到我需要的数据,但我不知道如何获取它的值。

我需要获取发件人对象内部的 "row" 值,该值已经非常隐藏,无法使用监视列表工具找到。 搜索了一段时间后,我找到了正确的值。 我在那里找到了它:
发件人 -> ActiveRecord -> 数据项 -> 行

Test.xaml.cs:

namespace WpfApplication1
{
    public partial class test : Window
    {
       public test()
       {
           InitializeComponent();
           runFASTWMVDataSetTableAdapters.StaatenTableAdapter staat = new runFASTWMVDataSetTableAdapters.StaatenTableAdapter();
           combo.ItemsSource = staat.GetData().DefaultView;          
       }
       //MainGrid2 is the grid which is in my combobox 
       private void MainGrid2_RecordActivated(object sender, Infragistics.Windows.DataPresenter.Events.RecordActivatedEventArgs e)
       {
            //combo is my comboBox object
            combo.IsDropDownOpen = false;   
           //guess i need to somehow read the "row" from the sender object right here. 
       }
    }
 }

我只 post xaml 的部分,这非常重要,因为我有一个很长的 xaml。

test.xaml:

 <igWPF:XamComboEditor x:Name="combo" Margin="1,1,1,219" ComboBoxStyle="{DynamicResource ComboStyle}" 
 DisplayValueSource="{Binding SelectedItem, RelativeSource={RelativeSource Self}}">  
    <Grid x:Name="MainGrid" SnapsToDevicePixels="True">
         <igWPF:XamDataGrid x:Name="MainGrid2"  DataSource="{TemplateBinding ItemsSource}"   
         RecordActivated="MainGrid2_RecordActivated" GroupByAreaLocation="None" SelectedDataItemsScope="RecordsOnly"/>  
    </Grid>
 </igWPF:XamComboEditor>

经过几个小时的努力和谷歌搜索后,我没有任何想法可以尝试,所以我想我可以向你们寻求帮助。 希望我在为 post 格式化代码时没有删除任何东西,它在我的本地项目中确实像预期的那样工作。
如果我的 post 有一些语法错误,也很抱歉,英语不是我的母语。

你问你的问题已经有一段时间了,但我想回答你的问题,即使你不再使用网格。也许这个答案会对其他人有所帮助。


我创建了一个示例,它将向您展示:

  1. 如何从发件人对象中获取特殊值?
  2. 如何将 XamDataGrid 的 ActiveRecord 绑定到 XamComboEditor 的 SelectedItem
  3. ComboBox 在 DataGrid 的嵌套类型中的另一种方法。

我的示例包含以下内容类:

Person.cs

namespace XamDataGridDemo
{
    public class Person
    {
        public int Id { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public int Age { get; set; }

        public override string ToString()
        {
            return $"{LastName}, {FirstName} ({Age})";
        }
    }
}

MainWindow.xaml

<Window x:Class="XamDataGridDemo.MainWindow"
        x:Name="Demo"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:ig="http://schemas.infragistics.com/xaml"
        xmlns:dp="http://infragistics.com/DataPresenter"
        xmlns:editors="http://infragistics.com/Editors"

        Title="XamDataGridDemo" Height="350" Width="525"
        DataContext="{Binding ElementName=Demo}"
        >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="3*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="3*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Reference InfragisticsWPF4.v16.1.dll -->
        <!-- Reference InfragisticsWPF4.DataPresenter.v16.1.dll -->
        <dp:XamDataGrid x:Name="FirstPersonGrid" 
                        Grid.Row="0"
                        DataSource="{Binding Path=Persons}"
                        RecordActivated="PersonGrid_OnRecordActivated"
                        />

        <Separator Grid.Row="1" Margin="3" />

        <Grid Grid.Row="2">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <!-- Reference InfragisticsWPF4.Editors.v16.1.dll -->
            <editors:XamComboEditor x:Name="Combo"
                                    Grid.Row="0"
                                    ItemsSource="{Binding Path=Persons}"
                                    SelectedItem="{Binding ElementName=SecondPersonGrid, Path=ActiveDataItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                    />

            <!-- Reference InfragisticsWPF4.DataPresenter.v16.1.dll -->
            <dp:XamDataGrid x:Name="SecondPersonGrid" 
                            Grid.Row="1"
                            DataSource="{Binding Path=Persons}"
                            />
        </Grid>

        <Separator Grid.Row="3" Margin="3" />

        <!-- Reference InfragisticsWPF4.Controls.Editors.XamComboEditor.v16.1.dll -->
        <ig:XamMultiColumnComboEditor Grid.Row="4"
                                      ItemsSource="{Binding Path=Persons}"
                                      EmptyText="Select a Person ..."
                                      SelectedValue="{Binding Path=SelectedItem.Id, RelativeSource={RelativeSource Self}}"
                                      SelectionChanged="XamMultiColumnComboEditor_OnSelectionChanged"
                                      />
    </Grid>
</Window>

MainWindow.xaml.cs

namespace XamDataGridDemo
{
    using System.Collections.Generic;
    using System.Windows;

    using Infragistics.Controls.Editors;
    using Infragistics.Windows.DataPresenter;
    using Infragistics.Windows.DataPresenter.Events;

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.Persons = new List<Person>
                               {
                                   new Person { Id = 0, FirstName = "Horst", LastName = "Horstenson", Age = 55 },
                                   new Person { Id = 1, FirstName = "Hilde", LastName = "Wild", Age = 45 },
                                   new Person { Id = 2, FirstName = "Klaus", LastName = "Klausen", Age = 50 }
                               };

            DataContext = this.Persons;
            InitializeComponent();
        }

        public IList<Person> Persons { get; }

        private void PersonGrid_OnRecordActivated(object sender, RecordActivatedEventArgs e)
        {
            var xamDataGrid = (XamDataGrid)sender;
            var activePerson = xamDataGrid.ActiveDataItem as Person;
            if (activePerson != null)
            {
                // if you want to do something with the properties
                int id = activePerson.Id;
                string firstName = activePerson.FirstName;
                string lastName = activePerson.LastName;
                int age = activePerson.Age;

                MessageBox.Show($"You have selected {activePerson}", "Selected Person");
            }

            // Second Approch -- Your Watchlist
            var record = xamDataGrid.ActiveRecord as DataRecord;
            activePerson = record?.DataItem as Person;
            if (activePerson != null)
            {
                MessageBox.Show($"You have selected {activePerson}", "Selected Person");
            }
        }

        private void XamMultiColumnComboEditor_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var xamMultiColumnComboEditor = (XamMultiColumnComboEditor)sender;
            var activePerson = xamMultiColumnComboEditor.SelectedItem as Person;

            if (activePerson == null)
            {
                return;
            }

            MessageBox.Show($"You have selected {activePerson}", "Selected Person");

            // Have to add reference to InfragisticsWPF4.DataManager.v16.1.dll
            if (xamMultiColumnComboEditor.SelectedValue is int)
            {
                int id = (int)xamMultiColumnComboEditor.SelectedValue;

                MessageBox.Show($"You have selected the person with id: {id}", "Selected Value");
            }
        }
    }
}

1.如何从发件人对象中获取特殊值?

正如您在 void PersonGrid_OnRecordActivated(object sender, RecordActivatedEventArgs e) 中看到的那样,我假设发件人是 XamDataGrid 并且我检查 ActiveDataItem 是否是我的类型 Person 的对象。
或者,如果您想遵循您的监视列表方法。您可以使用转换后的 sender 并检查 ActiveRecord 是否是我的 Person.

类型的 DataRecord

现在你可以用你的 "row" 对象做任何你想做的事了。

2。如何将 XamDataGrid 的 ActiveRecord 绑定到 XamComboEditor 的 SelectedItem?

我有点懒,所以我稍微缩短了第二个例子,并在 XamComboEditor 之外使用了 XamDataGrid,但它也应该在嵌套网格中工作,因为我使用带有 ElementName 的绑定。

如您所见,我只是将 XamComboEditor 的 SelectenItem 绑定到 XamDataGridActiveDataItem

SelectedItem="{Binding ElementName=SecondPersonGrid, Path=ActiveDataItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

我还使用 Mode=TwoWayUpdateSourceTrigger=PropertyChangedActiveDataItem 设置为 SelectedItem

3。 ComboBox 在 DataGrid 的嵌套类型中的另一种方法。

因为您已经尝试过 ,所以您应该看一下 XamMultiColumnComboEditor,顾名思义,它是一个包含您可以定义的列的组合框。 您可以使用与 1. 中相同的方法从发件人处获取选定的 Person。请参阅上面代码中的 void XamMultiColumnComboEditor_OnSelectionChanged(object sender, SelectionChangedEventArgs e)。 为了满足您的需要,为了获得 "row-id",我将 SelectedValue 绑定到所选 Person.

Id 属性
SelectedValue="{Binding Path=SelectedItem.Id, RelativeSource={RelativeSource Self}}"

我使用了 Infragistics v16.1 并且不得不在我的项目中添加以下引用。

  • InfragisticsWPF4.Controls.Editors.XamComboEditor.v16.1.dll
  • InfragisticsWPF4.DataManager.v16.1.dll
  • InfragisticsWPF4.DataPresenter.v16.1.dll
  • InfragisticsWPF4.Editors.v16.1.dll
  • InfragisticsWPF4.Editors.v16.1.dll

如果有什么不清楚或范围很广,请随时要求澄清。